1: Pre-sequence traversal (root, left, right)The recursive approach is simple:public static void Pr (TreeNode root) {if (root!=null) {System.out.println (root.val);p R (root.left);p R (root.right); }}Non-recursive method: You can use the stack, the order of the stack is right, left, root.The code is as follows:Public list2: Middle sequence traversal (left, Root, right)Recursive method:public static void M (TreeNode root) {if (root!=null) {m (root.left); System.out.println (Root.val); M (root.rig
=cur->Right ; +Stac.top (). second=2; -Stac.push (Make_pair (cur,0)); the } * ElseStac.pop ();//None of the above two can get in or go through or have no children . $ }Panax Notoginseng returnans; - } the};AC Code(4) More iterative method:1 /**2 * Definition for a binary tree node.3 * struct TreeNode {4 * int val;5 * TreeNode *left;6 * TreeNode *right;7 * TreeNode (int x): Val (x), left (null), right (null) {}8 * };9 */Ten classSolution { One Public
Given a binary tree, return the preorder traversal of its nodes 'values.
For example:Given Binary Tree{1,#,2,3},
1 2 / 3
Return[1,2,3].
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public ListIdea: Non-recursive forward Traversal
Re-build a tree from the central traversal + forward/backward traversal.
Iterator must be used before it can be used. Otherwise, MLE is used.
1. preorder + inorder
The first version uses the coordinate range:
1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */10 class Solution {11 public:12 Tree
[Question]
Given a binary tree, return the preorder traversal of its nodes 'values.
For example:Given Binary Tree{1,#,2,3},
1 2 / 3
Return[1,2,3].
Note: recursive solution is trivial, cocould You Do It iteratively?
Question]
Returns the first-order traversal result without recursion.
[Idea]
Maintain a stack.
[Code]
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right;
; order NBSP; by lft The result is c,d,e,f, and the order is correct. 4, get all the next Level 2 sub-node We have a example, this time with the LVL parameter, because a level is 1, so we query level is not greater than 3. 1Select* fromTreewhereLfT>2 andright One andlvl3Order byLfT Let's look at our new method for adding a node. Below the root node, we add an X node to the right of the G node The job we're going to do is 1, the right parameter of the G node is 13 2, change all the
Given a binary tree, return the preorder traversal of its nodes 'values.
For example:
Given Binary Tree {1, #, 2, 3 },
1 2 / 3
Return [1, 2, 3].
Note: Recursive solution is trivial, cocould You Do It iteratively?
Idea: Take the top element of the stack, output the element and exit it from the stack. If the right subtree is not empty, add the right subtree to the stack. If the left subtree is not empty, add the left subtree to the stack
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
Idea: Use the first-order traversal of the provided root node information, find the root node in the middle-order traversal, and then divide the sequence into {left subtree, root, right subtree }, recursively solve left and right subtree.
1 class Solution { 2 public: 3 TreeNode *buildTree( vector
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:You may assume that duplicates do not exist in the tree.
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode *buildTree(vector
Given a binary tree, returnPreorderTraversal of its nodes 'values.
For example:Given Binary Tree{1,#,2,3},
1 2 / 3
Return[1,2,3].
Note: recursive solution is trivial, cocould You Do It iteratively?
Solution 1: Non-recursive
1 /** 2 * Definition for binary tree 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 List
Solution 2: Recursion
1 /*
Label: des style color Io Java ar strong for div Given a binary tree, returnPreorderTraversal of its nodes 'values. For example:Given Binary Tree{1, #, 2, 3}, 1 2/3 Return[1, 2, 3]. Note:Recursive solution is trivial, cocould You Do It iteratively? Answer /*** Definition for binary tree * Public class treenode {* int val; * treenode left; * treenode right; * treenode (int x) {val = x ;} *} */public class solution {public list Binary Tree pre
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
1: Pay attention to special cases; 2: recursion; 3: recurrence end; 4: first obtain the root node, and then divide the two arrays into two parts, recursively obtain the left subtree and the right subtree.
TreeNode *buildTree(vector
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:You may assume that duplicates do not exist in the tree.
Idea: recursion. Pay attention to the start and end indexes of the call.
1/** 2 * definition for Binary Tree 3 * struct treenode {4 * int val; 5 * treenode * left; 6 * treenode * right; 7 * treenode (int x ): val (x), left (null), right (null) {} 8 *}; 9 */10 class solution {11 public: 12 treenode * buildtree (vector
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:You may assume that duplicates do not exist in the tree.
Similar http://www.cnblogs.com/sunshineatnoon/p/3854935.html
Only the pre-order and mid-order traversal sequences of the subtree are updated:
// Left subtree: left_prestart = prestart + 1left_preend = prestart + index-instart // right subtree right_prestart = prestart + index-instart + 1right_preend = preend
The Co
); A at if(cur->right) Stk.push (cur->Right ); - if(cur->left) Stk.push (cur->Left ); - - } - returnRes; - } in -};STK stores the reverse of the pending node, that is, top is the first node to be processed.After each processing top, the top of the right and left son push into Stk, so the next time is the top of the left son (then the first to deal with the top of the left sub-tree, in the subtree priority to the left subt
Title meaning: Two fork tree first sequence traversal, the result exists in vectorProblem-Solving ideas: 1. Recursion (the problem is that it doesn't make sense to use recursion, so I'll stick to the code)2. IterationIterative implementations:1 classSolution {2 Public:3vectorint> Preordertraversal (treenode*root) {4vectorint>ans;5 if(root) {6treenode*temp;7Stacks; Use stacks, each time you print the top of the stack, and then eject the stack top8 S.push (root);9 while(!S.em
The traversal of a treeI thought it was hard to die. Public classSolution { PublicArraylistpreordertraversal (TreeNode root) {ArrayListNewArraylist(); if(root==NULL)returnRes; StackNewStack(); St.add (root); while(!St.isempty ()) {TreeNode n=St.pop (); Res.add (N.val); if(n.right!=NULL) St.add (n.right); if(n.left!=NULL) St.add (n.left); } returnRes; }} Binary Tree Preorder Traversal
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.