Inorder
Binary Tree Inorder Traversal
lc题目链接:https://leetcode.com/problems...
recursion:
public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList();
dfs(root, result);
return result;
}
private void dfs(TreeNode root, List<Integer> result) {
// base case
if(root == null) return;
dfs(root.left, result);
result.add(root.val);
dfs(root.right, result);
}
}
iteration:
public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList();
// stack
Stack<TreeNode> stack = new Stack();
TreeNode node = root;
while(!stack.isEmpty() || node != null) {
while(node != null) {
stack.push(node);
node = node.left;
}
node = stack.pop();
result.add(node.val);
node = node.right;
}
return result;
}
}
morris: 参考这篇文章
http://www.cnblogs.com/AnnieK...
public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
/* morris: connect the node with its post if post != cur.next(cur.right == null)
* add current when cur.left == null
*/
List<Integer> result = new ArrayList();
TreeNode prev = null, cur = root;
while(cur != null) {
// reach the left most part, just add
if(cur.left == null) {
result.add(cur.val);
// right connect to current's post, null if finish traverse
cur = cur.right;
}
else {
prev = cur.left;
while(prev.right != null && prev.right != cur) prev = prev.right;
// connect prev with current: connect node with its post
if(prev.right == null) {
prev.right = cur;
cur = cur.left;
}
// recover the tree
else {
prev.right = null;
result.add(cur.val);
cur = cur.right;
}
}
}
return result;
}
}
Inorder Successor in BST
链接:https://leetcode.com/problems...
recursion:
public class Solution {
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
// base case
if(root == null) return null;
// left or root
if(p.val < root.val) {
TreeNode left = inorderSuccessor(root.left, p);
return left == null ? root : left;
}
else return inorderSuccessor(root.right, p);
}
}
iteration:
public class Solution {
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
TreeNode succ = null;
while(root != null) {
if(p.val < root.val) {
succ = root;
root = root.left;
}
else root = root.right;
}
return succ;
}
}
173. Binary Search Tree Iterator
题目链接:https://leetcode.com/problems...
还是一样的,iteration用stack,或者morris保存prev和cur。
public class BSTIterator {
Stack<TreeNode> stack;
public BSTIterator(TreeNode root) {
stack = new Stack();
// get the first node
getAllLeft(root);
}
private void getAllLeft(TreeNode node) {
while(node != null) {
stack.push(node);
node = node.left;
}
}
/** @return whether we have a next smallest number */
public boolean hasNext() {
return !stack.isEmpty();
}
/** @return the next smallest number */
public int next() {
if(!hasNext()) return -1;
TreeNode node = stack.pop();
if(node.right != null) getAllLeft(node.right);
return node.val;
}
}
Preorder
Binary Tree Preorder Traversal
题目链接:https://leetcode.com/problems...
recursion:
public class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
// recursion
List<Integer> result = new ArrayList();
dfs(root, result);
return result;
}
private void dfs(TreeNode root, List<Integer> result) {
// base case
if(root == null) return;
result.add(root.val);
dfs(root.left, result);
dfs(root.right, result);
}
}
iteration:
public class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
// iteration: use stack
List<Integer> result = new ArrayList();
if(root == null) return result;
Stack<TreeNode> stack = new Stack();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode cur = stack.pop();
result.add(cur.val);
if(cur.right != null) stack.push(cur.right);
if(cur.left != null) stack.push(cur.left);
}
return result;
}
}
morris:
public class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
// morris: connect cur with its post in inorder
// add as long as finding the post
List<Integer> result = new ArrayList();
TreeNode cur = root, prev = null;
while(cur != null) {
// left is null, need to go to right
if(cur.left == null) {
result.add(cur.val);
cur = cur.right;
}
else {
prev = cur.left;
// find the post in inorder
while(prev.right != null && prev.right != cur) prev = prev.right;
// connect with post and add
if(prev.right == null) {
prev.right = cur;
result.add(cur.val);
cur = cur.left;
}
// recover the tree
else {
prev.right = null;
cur = cur.right;
}
}
}
return result;
}
}
Postorder
Binary Tree Postorder Traversal
题目链接:https://leetcode.com/problems...
recursion:
public class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList();
dfs(root, result);
return result;
}
private void dfs(TreeNode root, List<Integer> result) {
if(root == null) return;
dfs(root.left, result);
dfs(root.right, result);
result.add(root.val);
}
}
iteration:
public class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList();
if(root == null) return result;
// right -> left -> reverse
Stack<TreeNode> stack = new Stack();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode cur = stack.pop();
result.add(cur.val);
if(cur.left != null) stack.push(cur.left);
if(cur.right != null) stack.push(cur.right);
}
Collections.reverse(result);
return result;
}
}
morris: 按照root -> right -> left的顺序最后再reverse一下。
public class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList();
// root -> right -> left
TreeNode cur = root, prev = null;
while(cur != null) {
// right == null, go to left
if(cur.right == null) {
result.add(cur.val);
cur = cur.left;
}
else {
prev = cur.right;
while(prev.left != null && prev.left != cur) {
prev = prev.left;
}
// connect according to inorder but right first: right->root->left
// leftmost.left = root
if(prev.left == null) {
result.add(cur.val);
prev.left = cur;
cur = cur.right;
}
else {
prev.left = null;
cur = cur.left;
}
}
}
Collections.reverse(result);
return result;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。