leetcode binary-tree-inorder-traversal(Java)

leetcode题目

binary-tree-inorder-traversal

题目描述

  • Given a binary tree, return the inorder traversal of its nodes’ values.

  • For example:

  • Given binary tree{1,#,2,3},

    1
    \
    2
    /
    3

  • return[1,3,2].

  • Note: Recursive solution is trivial, could you do it iteratively?

  • confused what"{1,#,2,3}"means? > read more on how binary tree is serialized on OJ.

  • OJ’s Binary Tree Serialization:

  • The serialization of a binary tree follows a level order traversal, where ‘#’ signifies a path terminator where no node exists below.

  • Here’s an example:

    1
    / \
    2 3
     /
     4
      \
      5

  • The above binary tree is serialized as"{1,2,3,#,#,4,#,#,5}".

思路

1、递归
2、一个栈缓存节点、同时利用当前处理的节点(初始指向根节点)实现

代码

package com.my.test.leetcode.tree;

import java.util.ArrayList;
import java.util.Stack;

/**
 * 题目:
 * binary-tree-inorder-traversal 
 *
 * 题目描述:
 * Given a binary tree, return the inorder traversal of its nodes' values.
 * For example:
 * Given binary tree{1,#,2,3},

   1
    \
     2
    /
   3

 * return[1,3,2].
 * Note: Recursive solution is trivial, could you do it iteratively?
 * confused what"{1,#,2,3}"means? > read more on how binary tree is serialized on OJ.
 * OJ's Binary Tree Serialization:
 * The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
 * Here's an example:

   1
  / \
 2   3
    /
   4
    \
     5
 * The above binary tree is serialized as"{1,2,3,#,#,4,#,#,5}".
 */
public class InOrderTree {

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }

    /**
     * 递归解法(栈溢出/内存溢出 风险,不推荐)
     */
    public ArrayList inorderRecursiveTraversal(TreeNode root) {
        ArrayList retList = new ArrayList<>();
        if (root == null) {
            return retList;
        }
        inorderRecursiveTraversal(root, retList);
        return retList;
    }
    
    private void inorderRecursiveTraversal(TreeNode root, ArrayList lst) {
        if (root == null) {
            return;
        }
        inorderRecursiveTraversal(root.left, lst);
        lst.add(root.val);
        inorderRecursiveTraversal(root.right, lst);
    }
    
    /**
     * 思路:
     * 1、利用栈缓存元素,初始当前节点指向根节点
     * 2、循环遍历树的元素,每次找到当前元素的最下面的左孩子节点,然后弹出栈中元素,记录到要返回的List中,同时把当前元素指向右孩子
     */
    public ArrayList inorderTraversal(TreeNode root) {
        ArrayList ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }
        
        Stack stack = new Stack<>();
        TreeNode cur = root;

        while (!stack.isEmpty() || cur != null) {
            // 找到左下方的元素
            while (cur != null) {
                // 添加当前访问的元素到栈中
                stack.push(cur);
                // 指针指向左孩子
                cur = cur.left;
            }
            
            // 弹出最下面的左孩子
            cur = stack.pop();
            // 当前元素值加入栈中
            ret.add(cur.val);
            // 当前节点指向右孩子
            cur = cur.right;
        }
        
        return ret;
    }
    
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode right = new TreeNode(2);
        root.right = right;
        TreeNode left = new TreeNode(3);
        right.left = left;
        System.out.println(new InOrderTree().inorderRecursiveTraversal(root));
        System.out.println(new InOrderTree().inorderTraversal(root));
        
        TreeNode root1 = new TreeNode(3);
        TreeNode left1 = new TreeNode(1);
        root1.left = left1;
        TreeNode right1 = new TreeNode(2);
        root1.right = right1;
        System.out.println(new InOrderTree().inorderRecursiveTraversal(root1));
        System.out.println(new InOrderTree().inorderTraversal(root1));
    }
}

你可能感兴趣的:(Leetcode题目)