Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree [1,null,2,3],
1
\
2
/
3
return [1,3,2].
1 /** 2 * Definition for a binary tree node. 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<Integer> inorderTraversal(TreeNode root) { 12 List<Integer> inorder = new ArrayList<>(); 13 if (root == null) { 14 return inorder; 15 } 16 Stack<TreeNode> stack = new Stack<>(); 17 TreeNode cur = root; 18 while (cur != null || !stack.empty()) { 19 while (cur != null) { 20 stack.push(cur); 21 cur = cur.left; 22 } 23 cur = stack.pop(); 24 inorder.add(cur.val); 25 cur = cur.right; 26 } 27 return inorder; 28 } 29 }

浙公网安备 33010602011771号