Sum Root to Leaf Numbers

Source: Internet
Author: User

Sum Root to Leaf Numbers 

Given a binary tree containing digits from only 0-9 , 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

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 = + = 25 .

This problem, I was on the basis of sequential traversal, added a similar stack of the list, if the non-leaf node will be "into the stack", if it is a leaf node "stack" until node's father nodes appear, scan "bottom" to "stack top" all elements of the number, calculate sum. Until the end of the pre-order traversal

1 Importjava.util.ArrayList;2 Importjava.util.List;3 4 5 6 7    classTreeNode {8       intVal;9 TreeNode left;Ten TreeNode right; OneTreeNode (intX) {val =x;} A   } -  -  Public classSolution { the  -list<treenode> list =NewArraylist<treenode>(); -      -     intsum = 0; +      -      Public intsumnumbers (TreeNode root) { +         if(NULL= = root)//empty tree returns 0 A             return0; at         Else if(NULL= = Root.left &&NULL= = Root.right) {//only the root node returns the value of the root node -             returnRoot.val; -}Else{ -Preorder (root);//there is at least one sub-tree - //List.add (root); //root node into the stack -         } in          -         returnsum; to     } +      -     /** the * Pre-sequence traversal tree *      * @paramRoot $      */Panax Notoginseng      Public voidpreorder (TreeNode root) { -         if(Root = =NULL) the             return; +         if(NULL= = Root.left && Root.right = =NULL){//leaf node A              while(List.size ()! = 0 && Root! = List.get (List.size ()-1). Left && root! = List.get (List.size ()-1). right) { theList.remove (List.size ()-1); +             } -             inttemp = 0; $              for(inti = 0; I < list.size (); i++) {//calculates a number $temp = temp * 10 +List.get (i). val; -             } -Temp = temp * + root.val;//leaf node plus go the             //System.out.println ("temp =" + temp); -sum + = temp;//Calculation andWuyi             if(Root = = List.get (List.size ()-1). right) {//if the leaf node is the right sub-tree, the stack theList.remove (List.size ()-1); -             } Wu         } -         Else{//Non-leaf node About              while(List.size ()! = 0 && Root! = List.get (List.size ()-1). Left && root! = List.get (List.size ()-1). right) { $List.remove (List.size ()-1); -}//root should be the same as the top element of the stack - List.add (root); - preorder (root.left); A preorder (root.right); +         } the     } -}

The topic is to use DFS, a while Baidu a bit ~

Look at the inside of discuss, this is very beautiful written

 Public classSolution { Public intsumnumbers (TreeNode root) {returnPreorder (Root, 0); }         Public intPreorder (TreeNode root,intval) {        if(Root = =NULL)            return0; Val= val * 10 +Root.val; if(NULL= = Root.left &&NULL==root.right)returnVal; Else            returnPreorder (Root.left, Val) +Preorder (Root.right, Val); }}

Sum Root to Leaf Numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.