94. 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].

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

題意:

對一個二叉樹做一個中序歷遍(請用迭代的方式)

題解(遞歸):

雖然題目要求使用迭代的方式,但我們還是先以遞歸的方式歷遍,以便於理解。遞歸的方式非常直觀,是以左(root.left)->中(root)->右(root.right)的方式來依序拜訪每個樹的節點,可以很容易地寫出下面的代碼:

package LeetCode.Medium;

import java.util.ArrayList;
import java.util.List;

import LeetCode.Dependencies.TreeNode;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 * 左中右(遞歸版)
 */
public class BinaryTreeInorderTraversal_Recursive  {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if(root == null)
            return result;
        
        helper(root, result);
        
        return result;
    }
    
    void helper(TreeNode root, List<Integer> result) {
    	//若此節點沒有值則返回
        if(root == null)
            return;
        
        //進行左子樹的歷遍
        helper(root.left, result);
        
        //將此節點放入結果當中
        result.add(root.val);
        
        //進行右子樹的歷遍
        helper(root.right, result);
    }
}

題解(迭代):

但依據題目要求,我們仍需要使用迭代來解這一道題,故我們利用Stack(棧)的思想來解這道題,棧的思想是先進後出,我們可以寫出下面的迭代步驟:

1.將首節點加入stack中
 =====(直到stack為空)========
 2.if 當前節點不為空(還沒拜訪到當前子樹的最左節點)
      2.1將root.left放入stack
      2.2將root指定為root.left
    else(繼續拜訪右子樹)
      2.1將root指定為stack排出的節點
      2.2將root的值加入結果
      2.3將root的值指定為root.right
 =====(stack為空後)==========
 3.將結果的最後一個值(多出的)刪除
 4.輸出結果

ex:

  4                             stack     result
 /  \            =============================
2  5           step0      4           null
/ \               step1      4,4        null
      1   3             step2      4,4,2     null
                          step3      4,4,2,1  null
                          step4      4,4,2     1
                          step5      4,4,3     1,2
                          step6      4,5        1,2,3
                          step7      4           1,2,3,4
                          step8      null       1,2,3,4,5,4
                          step9      null       1,2,3,4,5

package LeetCode.Medium;

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

import LeetCode.Dependencies.TreeNode;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 * 左中右(迭代版)
 */
public class BinaryTreeInorderTraversal {
	/*
	 * 1.將首節點加入stack中
	 * =====(直到stack為空)========
	 * 2.if 當前節點不為空(還沒拜訪到當前子樹的最左節點)
	 *      2.1將root.left放入stack
	 *      2.2將root指定為root.left
	 *   else(繼續拜訪右子樹)
	 *   	2.1將root指定為stack排出的節點
	 *      2.2將root的值加入結果
	 *      2.3將root的值指定為root.right
	 * =====(stack為空後)==========
	 * 3.將結果的最後一個值(多出的)刪除
	 * 4.輸出結果
	ex:
	   4          stack     result
	  / \   =============================
	 2   5  step0  4        null
	/ \     step1  4,4      null
   1   3    step2  4,4,2    null
            step3  4,4,2,1  null
            step4  4,4,2    1
            step5  4,4,3    1,2
            step6  4,5      1,2,3
            step7  4        1,2,3,4
            step8  null     1,2,3,4,5,4
            step9  null     1,2,3,4,5
	 */
      public List<Integer> inorderTraversal(TreeNode root) {
    	//用來存儲結果
        List<Integer> result = new ArrayList<>();
        
        //對於root == null的情況
        if(root == null)
            return result;
        
        //利用stack來進行樹的歷遍
        Stack<TreeNode> stack = new Stack<>();
        
        //一定要將第一個節點放入stack中
        stack.push(root);
        
        while(stack.isEmpty() == false) {
        	//若root不為空的情況(還沒拜訪到當前子樹的最左節點)
            if(root != null) {
                stack.push(root);
                root = root.left;
          
            //若root為空的情況(繼續拜訪右子樹)
            } else if(root == null){
                root = stack.pop();
                result.add(root.val);
                root = root.right;
            }
        }
        
        //將結果的最後一個值(多出的)刪除
        result.remove(result.size() - 1);

        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值