zoukankan      html  css  js  c++  java
  • 404. Sum of Left Leaves

    Find the sum of all left leaves in a given binary tree.

    左树的值(9+15=24)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public int sumOfLeftLeaves(TreeNode root) {
            if(root==null){
                return 0;
            };
            int ans=0;
            Stack<TreeNode> stack=new Stack<TreeNode>();
            stack.push(root);
            
            while(!stack.empty()){
                TreeNode node=stack.pop();
                
                if(node.left!=null&&node.left.left==null&&node.left.right==null){
                    
                        ans+=node.left.val;
                       
                    }
               if(node.right!=null){
                     
                           stack.push(node.right);
                }
                if(node.left!=null){
                     
                           stack.push(node.left);
                }
               
            }
                 
             return ans;
            
        }
    }
  • 相关阅读:
    注册系统
    android登录界面
    android作业 购物界面
    第六周jsp作业
    JSP第四周
    JSP第二次作业
    JSP第一次作业
    第一周软件测试
    第九次安卓
    购物菜单
  • 原文地址:https://www.cnblogs.com/sunli0205/p/6089105.html
Copyright © 2011-2022 走看看