Programs from Program Creek
Ago
Preorder binary tree traversal is a classic interview problem about trees. The key to solve this problem are to understand the following:
- What is preorder? (parent node is processed before it children)
- Use Stack from the Java Core Library
It isn't obvious what preorder for some strange cases. However, if you draw a stacks and manually execute the program, what each element was pushed and popped is obvious.
The key to solve this problem be using a stack to store left and right children Processed after the left child.
1 Public classTreeNode {2 intVal;3 TreeNode left;4 TreeNode right;5TreeNode (intX) {val =x;}6 }7 8 Public classSolution {9 PublicArraylist<integer>preordertraversal (TreeNode root) {Tenarraylist<integer> returnlist =NewArraylist<integer>(); One A if(Root = =NULL) - returnreturnlist; - thestack<treenode> stack =NewStack<treenode>(); - Stack.push (root); - - while(!Stack.empty ()) { +TreeNode n =Stack.pop (); - Returnlist.add (n.val); + A if(N.right! =NULL){ at Stack.push (n.right); - } - if(N.left! =NULL){ - Stack.push (n.left); - } - in } - returnreturnlist; to } +}
In
The key to solve inorder traversal of binary tree includes the following:
- The order of "inorder" Is:left Child, Parent
- Use a stack to track nodes
- Understand when to push node into the stack and when to POPs node out of the stack
1 //Definition for binary tree2 Public classTreeNode {3 intVal;4 TreeNode left;5 TreeNode right;6TreeNode (intX) {val =x;}7 }8 9 Public classSolution {Ten PublicArraylist<integer>inordertraversal (TreeNode root) { One //Important:please Reset any member data declared, as A //The same solution instance would be a reused for each test case. -arraylist<integer> LST =NewArraylist<integer>(); - the if(Root = =NULL) - returnlst; - -stack<treenode> stack =NewStack<treenode>(); + //define a pointer to track nodes -TreeNode p =Root; + A while(!stack.empty () | | P! =NULL){ at - //if it is not NULL, the push to stack - //And go down the tree to left - if(P! =NULL){ - Stack.push (p); -p =P.left; in - //If no left child to //Pop stack, process the node + //Then let p -}Else{ theTreeNode T =Stack.pop (); * Lst.add (t.val); $p =T.right;Panax Notoginseng } - } the + returnlst; A } the}
After
The key to iterative Postorder traversal is the following:
- The order of "Postorder" is:left Child, right child, parent node.
- Find the relation between the previously visited node and the current node
- Use a stack to track nodes
As we go down the tree, check the previously visited node. If It is the parent of the "current" node, we should add current node to stack. When there are no children for the current node, the pop it from stack. Then the previous node become to is under the current node for Next loop.
1 //Definition for binary tree2 Public classTreeNode {3 intVal;4 TreeNode left;5 TreeNode right;6TreeNode (intX) {val =x;}7 }8 9 Ten Public classSolution { One PublicArraylist<integer>postordertraversal (TreeNode root) { A -arraylist<integer> LST =NewArraylist<integer>(); - the if(Root = =NULL) - returnlst; - -stack<treenode> stack =NewStack<treenode>(); + Stack.push (root); - +TreeNode prev =NULL; A while(!Stack.empty ()) { atTreeNode Curr =Stack.peek (); - - //go down the tree. - //Check if current node was leaf, if so, process it and pop stack, - //Otherwise, keep going down - if(prev = =NULL|| Prev.left = = Curr | | Prev.right = =Curr) { in //prev = = NULL is the situation for the root node - if(Curr.left! =NULL){ to Stack.push (curr.left); +}Else if(Curr.right! =NULL){ - Stack.push (curr.right); the}Else{ * Stack.pop (); $ Lst.add (curr.val);Panax Notoginseng } - the //go up the tree from left node + //need to check if there A //If yes, push it to stack the //Otherwise, process parent and pop stack +}Else if(Curr.left = =prev) { - if(Curr.right! =NULL){ $ Stack.push (curr.right); $}Else{ - Stack.pop (); - Lst.add (curr.val); the } - Wuyi //go up the tree from right node the //After coming back from the right node, process parent node and pop stack. -}Else if(Curr.right = =prev) { Wu Stack.pop (); - Lst.add (curr.val); About } $ -Prev =Curr; - } - A returnlst; + } the}
Yet to be practiced and collated
Java data Structure Four--two fork tree's front, middle, and post-sequential traversal