(一)Binary Tree Preorder Traversal
https://leetcode.com/problems/binary-tree-preorder-traversal/description/
题目:给定二叉树,返回其前序遍历(根左右);
解答:使用栈或者递归;
代码:
*************栈****************
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
if (root == null) {
return res;
}
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
res.add(node.val);
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
return res;
}
}
************递归***************
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
recursion(res, root);
return res;
}
private void recursion(List<Integer> list, TreeNode node) {
if (node == null) {
return;
}
list.add(node.val);
if (node.left != null) {
recursion(list, node.left);
}
if (node.right != null) {
recursion(list, node.right);
}
}
}
(二)Binary Tree Inorder Traversal
https://leetcode.com/problems/binary-tree-inorder-traversal/description/
题目:给定二叉树,返回其中序遍历(左根右);
解答:使用栈;
代码:
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new LinkedList<>();
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();
res.add(node.val);
node = node.right;
}
return res;
}
}
(三)Binary Tree Postorder Traversal
https://leetcode.com/problems/binary-tree-postorder-traversal/description/
题目:给定二叉树,返回其后序遍历(左右根);
解答:使用栈或者递归;
代码:
**********************递归*************************
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) {
return res;
}
res.addAll(postorderTraversal(root.left));
res.addAll(postorderTraversal(root.right));
res.add(root.val);
return res;
}
}
***********************栈*******************************
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) {
return res;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
TreeNode prev = null;
while (!stack.isEmpty()) {
TreeNode curr = stack.peek();
if (prev == null || prev.right == curr || prev.left == curr) {
if (curr.left != null) {
stack.push(curr.left);
} else if (curr.right != null) {
stack.push(curr.right);
}
} else if (curr.left == prev) {
if (curr.right != null) {
stack.push(curr.right);
}
} else {
res.add(curr.val);
stack.pop();
}
prev = curr;
}
return res;
}
}
(四)Binary Search Tree Iteration
https://leetcode.com/problems/binary-search-tree-iterator/description/
题目:实现二叉查找树迭代器,next()返回下一个最小值。要求next()和hasNext()函数的时间、空间复杂度分别为O(1)和O(h),h表示树的高度;
解答:(二叉查找树为有序二叉树,左节点均小于根节点,右节点均大于根节点)因此,最小的数即为最左的节点。
1、将二叉树中所有左节点压入栈中(初始化);
2、若栈为空, hasNext返回真;
3、当前栈的最上面元素pop出来,即为下一个最小值;此时需要判断pop出的节点是否存在左子树,若存在,则需要将其左子树的所有左节点pop入栈(为下一个最小值做 准备)。
代码:
public class BSTIterator {
Stack<TreeNode> stack = new Stack<>();
public BSTIterator(TreeNode root) {
addAllLeft(root);
}
/** @return whether we have a next smallest number */
public boolean hasNext() {
return !stack.isEmpty();
}
/** @return the next smallest number */
public int next() {
TreeNode node = stack.pop();
if (node.right != null) {
addAllLeft(node.right);
}
return node.val;
}
private void addAllLeft(TreeNode node) {
while (node != null) {
stack.push(node);
node = node.left;
}
}
}