Given a binary tree, return the inorder traversal of its nodes' values.
Example:
Input: [1,null,2,3] 1 2 / 3 Output: [1,3,2]
用Stack,左根右,push左,左=左。根。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public List<Integer> inorderTraversal(TreeNode root) { ArrayList<Integer> result = new ArrayList<>(); Stack<TreeNode> s = new Stack<>(); TreeNode p = root; while (!s.empty() || p != null) { if (p != null) { s.push(p); p = p.left; } else { p = s.pop(); result.add(p.val); p = p.right; } } return result; } }