94. 二叉树的中序遍历


题目: https://leetcode-cn.com/problems/binary-tree-inorder-traversal/ 解法:

  1. 中序遍历:左根右
  2. 利用栈
public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> integerList = new ArrayList<>();
        Stack<TreeNode> treeNodeStack = new Stack<>();
        while (root != null || !treeNodeStack.isEmpty()) {
            while (root != null) {
                treeNodeStack.push(root);
                root = root.left;
            }
            if (!treeNodeStack.isEmpty()) {
                root = treeNodeStack.pop();
                integerList.add(root.val);
                root = root.right;
            }

        }

        return integerList;
}
MySql
Algorithm

评论