My code:
import java.util.ArrayList;
import java.util.List;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> result = new ArrayList<Integer>();
if (root == null)
return result;
preorder(root, result);
return result;
}
private void preorder(TreeNode root, ArrayList<Integer> result) {
result.add(root.val);
if (root.left != null)
preorder(root.left, result);
if (root.right != null)
preorder(root.right, result);
}
}
My test result:
考先序遍历的,没什么好说的。
非递归写法:
public List<TreeNode> preOrder(TreeNode root) {
if (root == null)
return null;
TreeNode p = root;
Stack<TreeNode> s = new Stack<TreeNode>();
ArrayList<TreeNode> result = new ArrayList<TreeNode>();
while (p != null || !s.isEmpty()) {
while (p != null) {
result.add(p);
s.push(p);
p = p.left;
}
if (!s.isEmpty()) {
p = s.pop();
p = p.right;
}
}
return result;
}
**
总结: pre order tree
**
Anyway, Good luck, Richardo!
My code:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> ret = new ArrayList<Integer>();
if (root == null)
return ret;
Stack<TreeNode> s = new Stack<TreeNode>();
TreeNode node = root;
while (node != null) {
ret.add(node.val);
if (node.right != null)
s.push(node.right);
node = node.left;
if (node == null && !s.isEmpty()) {
node = s.pop();
}
}
return ret;
}
}
这是我的写法。就是每次访问当前结点,然后如果right child != null, 压入栈中。
curr = curr.left;
然后判断 curr 是否为 null.如果是,表示走到头了。那就从栈中弹出元素,开始访问右侧。
注意循环判断条件得是 node != null 而不是栈是否为空。
Anyway, Good luck, Richardo!
My code:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<Integer>();
if (root == null) {
return ret;
}
Stack<TreeNode> st = new Stack<TreeNode>();
TreeNode p = root;
while (p != null || !st.isEmpty()) {
while (p != null) {
ret.add(p.val);
st.push(p);
p = p.left;
}
p = st.pop().right;
}
return ret;
}
}
复习了下 iteration的写法,但还是没能写出来,看了答案。
Anyway, Good luck, Richardo! -- 09/06/2016
Morris traversal:
My code:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<Integer>();
if (root == null) {
return ret;
}
TreeNode curr = root;
while (curr != null) {
if (curr.left == null) {
ret.add(curr.val);
curr = curr.right;
}
else {
TreeNode pre = curr.left;
while (pre.right != null && pre.right != curr) {
pre = pre.right;
}
if (pre.right == null) {
ret.add(curr.val);
pre.right = curr;
curr = curr.left;
}
else {
pre.right = null;
curr = curr.right;
}
}
}
return ret;
}
}
reference:
http://www.cnblogs.com/AnnieKim/archive/2013/06/15/morristraversal.html
Anyway, Good luck, Richardo! -- 09/08/2016