leetcode--Sum Root to Leaf Numbers

来源:互联网 发布:翻译软件哪个好 编辑:程序博客网 时间:2024/05/29 08:27

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 number123.

Find the total sum of all root-to-leaf numbers.

For example,

    1   / \  2   3

The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.

Return the sum = 12 + 13 = 25.


分类:二叉树

题意:二叉树每条路径上的数组合成一个数,求这些数相加的总和


解法1:非递归后续遍历,每次遍历到叶子节点,计算当前栈内,所有节点(也就是这条路径)所代表的数,加上总和。

[java] view plain copy
  1. /** 
  2.  * Definition for a binary tree node. 
  3.  * public class TreeNode { 
  4.  *     int val; 
  5.  *     TreeNode left; 
  6.  *     TreeNode right; 
  7.  *     TreeNode(int x) { val = x; } 
  8.  * } 
  9.  */  
  10. public class Solution {  
  11.     public int sumNumbers(TreeNode root) {  
  12.         if(root==nullreturn 0;  
  13.         LinkedList<TreeNode> queue = new LinkedList<TreeNode>();  
  14.         TreeNode cur = root;  
  15.         int sum = 0;  
  16.         do{       
  17.             while(cur!=null){  
  18.                 queue.add(cur);  
  19.                 cur = cur.left;  
  20.             }             
  21.             TreeNode p = null;  
  22.             boolean flag = true;  
  23.             while(flag&&queue.size()>0){  
  24.                 cur = queue.peekLast();  
  25.                 if(cur.right==p){  
  26.                     if(cur.left==null&&cur.right==null){  
  27.                         int t_sum = 0;  
  28.                         for(TreeNode t:queue){  
  29.                             t_sum = t_sum*10+t.val;  
  30.                         }                         
  31.                         sum += t_sum;  
  32.                     }  
  33.                     p = cur;  
  34.                     queue.pollLast();  
  35.                 }else{  
  36.                     cur = cur.right;  
  37.                     flag = false;  
  38.                 }  
  39.             }             
  40.         }while(queue.size()>0);  
  41.         return sum;  
  42.     }  
  43. }  


解法2:深度搜索。关键在于,对于每个节点,我们要知道它之前和是多少。

[java] view plain copy
  1. /** 
  2.  * Definition for a binary tree node. 
  3.  * public class TreeNode { 
  4.  *     int val; 
  5.  *     TreeNode left; 
  6.  *     TreeNode right; 
  7.  *     TreeNode(int x) { val = x; } 
  8.  * } 
  9.  */  
  10. public class Solution {  
  11.     public int sumNumbers(TreeNode root) {  
  12.         return dfs(root,0);  
  13.     }  
  14.       
  15.     /** 
  16.      * cursum为root之前的总和  
  17.      */  
  18.     public int dfs(TreeNode root,int cursum){  
  19.         if(root==nullreturn 0;  
  20.         if(root.left==null&&root.right==null){//如果是叶子节点  
  21.             return cursum*10+root.val;//总和就是之前的*10+当前值  
  22.         }  
  23.         return dfs(root.left,10*cursum+root.val)+dfs(root.right,10*cursum+root.val);  
  24.     }  
  25. }  

原文链接http://blog.csdn.net/crazy__chen/article/details/46523567