LeetCode125 Sum Root to Leaf Numbers

Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path
could represent a number.

An example is the root-to-leaf path 1->2->3 which represents the number 123.

解题思想:二叉树遍历,判断如果是叶子节点,就把路径的节点转换为整数。这里面我用了二个栈

stack用于返回上个节点

stack1用于记录每次根节点到叶子的所有节点,stack1中节点和stack一样添加节点,但是弹出的时机不一样。要注重分析。

<span style="color:#333333;"> public int sumNumbers(TreeNode root) {
        if(root==null)return 0;
        Stack<TreeNode> stack=new Stack<TreeNode>();
        Stack<TreeNode> stack1=new Stack<TreeNode>();
        StringBuilder builder=new StringBuilder();
        int sum=0;
        TreeNode current=root;
        while(current!=null||!stack.isEmpty())
        {
            if(current!=null)
            {
                stack.push(current);
                stack1.push(current);
                current=current.left;
            }else{
                current=stack.pop();
                if(current.right==null&¤t.left==null)
                {
                    for(int i=0;i<stack1.size();i++)
                    {
                    builder.append(stack1.get(i).val);
                    }
                    sum+=Integer.parseInt(builder.toString());
                    builder.delete(0, builder.length());
                    </span><span style="color:#ff0000;">stack1.pop();//叶子节点计算完弹出</span><span style="color:#333333;">
                }else{
                    </span><span style="color:#ff0000;">while(!stack1.isEmpty())
                    {
                     if(stack1.peek()==current)break;//当current存在右节点时stack中的current的节点已经弹出
                     stack1.pop();                  //但是stack1中的current节点不能弹出,我们要保持路径上的完整节点
                    }</span><span style="color:#333333;">
                }
                current=current.right;
            }
        }
        return sum;
    }</span>
时间: 2024-08-19 05:13:39

LeetCode125 Sum Root to Leaf Numbers的相关文章

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

Sum Root to Leaf Numbers

Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf number

leetcode --day12 Surrounded Regions &amp; Sum Root to Leaf Numbers &amp; Longest Consecutive Sequence

1.  Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your fu

LeetCode: Sum Root to Leaf Numbers 解题报告

Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf number

23. Sum Root to Leaf Numbers

Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf number

【LeetCode OJ】Sum Root to Leaf Numbers

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 # Definition for a  binary tree node # class TreeNode: #     def __init__(self, x): #         self.val = x #         self.left = No

Sum Root to Leaf Numbers leetcode java

题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2

[leetcode]Sum Root to Leaf Numbers @ Python

原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Fin