LeetCode94. 二叉树的中序遍历🌟🌟🌟🌟简单

课后作业

问题描述

原文链接:94. 二叉树的中序遍历

给定一个二叉树的根节点 root ,返回它的中序遍历 。

示例 1:

img

输入:root = [1,null,2,3]
输出:[1,3,2]

示例 2:

输入:root = []
输出:[]

示例 3:

输入:root = [1]
输出:[1]

提示:

  • 树中节点数目在范围 [0, 100] 内
  • -100 <= Node.val <= 100

代码实现

Java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        if(root == null){
            return new ArrayList<>();
        }

        Stack<TreeNode> stack = new Stack();
        List<Integer> res = new ArrayList();

        while(!stack.isEmpty() || root != null){
            if(root != null){
                stack.push(root);
                root = root.left;
            }else{
                TreeNode temp = stack.pop();
                res.add(temp.val);
                root = temp.right;
            }
        }

        return res;
    }
}

Python

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def inorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if root is None:
            return []

        stack = []
        res = []

        while len(stack) > 0 or root:
            if root:
                stack.append(root)
                root = root.left
            else:
                temp = stack.pop()
                res.append(temp.val)
                root = temp.right

        return res

C++

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        if(root == nullptr){
            return {};
        }

        stack<TreeNode*> st;
        vector<int> res;

        while(!st.empty() || root != nullptr){
            if(root != nullptr){
                st.push(root);
                root = root->left;
            }else{
                TreeNode* temp = st.top();
                st.pop();
                res.push_back(temp->val);
                root = temp->right;
            }
        }

        return res;
    }
};

Go

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func inorderTraversal(root *TreeNode) []int {
    if root == nil{
        return []int{}
    }

    var stack []*TreeNode
    var res []int

    for len(stack) > 0 || root != nil{
        if root != nil{
            stack = append(stack, root)
            root = root.Left
        }else{
            temp := stack[len(stack)-1]
            stack = stack[:len(stack)-1]
            res = append(res, temp.Val)
            root = temp.Right
        }
    }

    return res
}

发表回复

后才能评论

评论(4)

  • control 普通 2024年12月11日 下午9:59

    class Solution {
    public List inorderTraversal(TreeNode root) {
    ArrayList
    re = new ArrayList();
    this.inOrder(root, re);
    return re;

    }
    
    public void inOrder(TreeNode node , ArrayList<Integer> result){
        // 先左,后父,最后右
        if(node != null ){
            this.inOrder(node.left, result);
            result.add(node.val);
            this.inOrder(node.right,result);
        }else{
            return;
        }
    
    
    }
    

    }
    中序遍历

  • 阿尚 普通 2023年8月21日 上午10:25
    //中序遍历
    class Solution {
        public List<Integer> inorderTraversal(TreeNode root) {
            List<Integer> result = new ArrayList<>();
            if(root == null) return result;
    
            Stack<TreeNode> stack = new Stack<>();
            TreeNode p = root;
    
            while(p != null || !stack.isEmpty()){
                if(p != null){
                    stack.push(p);
                    p = p.left;
                }else{
                    TreeNode node = stack.pop();
                    result.add(node.val);
                    p = node.right;
                }
            }
    
            return result;
        }
    }
    
  • 帅气的冬瓜 普通 2023年7月4日 上午10:32

    打卡。这个虽然能写的出,但是逻辑好难理解啊,太绕了。我自己写出来就像是背的,思维逻辑没整明白,估计以后还是会忘写不出。

如果您已经成功付款但是网站没有弹出成功提示,请联系站长提供付款信息为您处理