首页 > 其他分享 >LeetCode 94 二叉树的中序遍历

LeetCode 94 二叉树的中序遍历

时间:2023-03-31 17:22:36浏览次数:34  
标签:right cur res 中序 LeetCode 遍历 二叉树 root 节点

LeetCode | 94.二叉树的中序遍历

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

示例 1:

图片名称
输入:root = [1,null,2,3]
输出:[1,3,2]
示例 2:

输入:root = []
输出:[]
示例 3:

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

提示:

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

迭代实现:

vector<int> inorderTraversal(TreeNode* root) {
    vector<int> res;
    stack<TreeNode*> st;
    while (!st.empty() || root) {
        while (root) {
            st.push(root); 
            root = root->left;
        }
        root = st.top();
        st.pop();
        res.push_back(root->val);
        root = root->right;
    }
    return res;
}

递归实现:

void helper(TreeNode* root, vector<int> &res) {
    if (root->left) 
        helper(root->left, res);
    res.push_back(root->val);
    if (root->right) 
        helper(root->right, res);
}

vector<int> inorderTraversal(TreeNode* root) {
    if (!root) 
        return {};

    vector<int> res;
    helper(root, res);

    return res;
}

mirrors 算法:
设当前节点为 cur
没有左节点
 直接遍历右节点
有左节点
 找到左节点的最右的子节点
  如果最右节点的右节点为空 (最右节点的右节点不一定为空,因为我们会令它指向 cur 节点)
   令右节点指向 cur, 遍历 cur 的左节点
  如果最右节点的右节点不为空,说明 cur 的左节点已经遍历完
   断开最右节点和 cur 的连接,开始遍历 cur 的右节点

vector<int> inorderTraversal(TreeNode* root) {
    vector<int> res;
    while (root) {
        if (root->left) { // 此时的 root 表示 cur 节点
            TreeNode* rightmost = root->left;
            while (rightmost->right && rightmost->right != root) {
                rightmost = rightmost->right; //找到左节点的最右节点
            }
            if (rightmost->right) { // 最右节点的右节点不为空,cur 节点的左节点遍历完
                res.push_back(rightmost->right->val);
                root = root->right;
                rightmost->right = nullptr;
            } else { // 最右节点为空,令其指向 root,开始遍历左节点
                rightmost->right = root;
                root = root->left;
            }
        } else { // 左节点为空,直接遍历右节点
            res.push_back(root->val);
            root = root->right;
        }
    }
    return res;
}  

标签:right,cur,res,中序,LeetCode,遍历,二叉树,root,节点
From: https://www.cnblogs.com/AngleLin/p/17276930.html

相关文章

  • 反转链表-leetcode92
    给你单链表的头指针head和两个整数left和right,其中left<=right。请你反转从位置left到位置right的链表节点,返回反转后的链表。示例1:输入:head=[1,2,3,4,5],left=2,right=4输出:[1,4,3,2,5]示例2:输入:head=[5],left=1,right=1输出:[5]//leet......
  • 之子形打印二叉树
    classSolution{public:vector<vector<int>>res;voidbfs(TreeNode*root){queue<TreeNode*>q;q.push(root);intlevel=0;while(q.size()){intsize=q.size();......
  • 不分行从上往下打印二叉树
    classSolution{public:vector<int>res;voidbfs(TreeNode*root){queue<TreeNode*>q;q.push(root);while(q.size()){autop=q.front();q.pop();res.push_back(......
  • Leetcode19. 删除链表的倒数第 N 个结点
     19. 删除链表的倒数第N个结点自己纯手写第一题,递归有点冗杂,开辟了虚拟头节点,而且要特别注意边界条件(当倒数第n个正好是头节点时)。***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode():val(0),n......
  • 上下翻转二叉树
    给定一个二叉树的根节点root,按照如下的方式上下翻转二叉树,并返回新的根节点。1、原来的左子节点变成新的根节点2、原来的根节点变成新的右子节点3、原来的右子节点变成新的左子节点上面的步骤都是逐层进行的,题目数据保证每个右节点都有一个同级节点(共享同一个父节点的左......
  • JZ7 重建二叉树
     JZ7 重建二叉树方法一:递归做法前序的第一个结点就是根节点,而后在中序中将根节点的位置找出,根节点的左边是左子树,右边是右子树,而后再带入前序中分析,形成迭代。/***Definitionforbinarytree*structTreeNode{*intval;*TreeNode*left;*Tree......
  • LeetCode 559.二叉树的最大深度
    1.题目:给定一个N叉树,找到其最大深度。最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。N叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。示例1:输入:root=[1,null,3,2,4,null,5,6]输出:3来源:力扣(LeetCode)链接:https://leetcode.cn/problems/maximum......
  • LeetCode 222.完全二叉树的结点个数
    1.题目:给你一棵完全二叉树的根节点root,求出该树的节点个数。完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第h层,则该层包含1~ 2h 个节点。示例1:输入:root=[1......
  • leetcode-1089-easy
    DuplicateZerosGivenafixed-lengthintegerarrayarr,duplicateeachoccurrenceofzero,shiftingtheremainingelementstotheright.Notethatelementsbe......
  • leetcode-1317-easy
    ConvertIntegertotheSumofTwoNo-ZeroIntegersNo-Zerointegerisapositiveintegerthatdoesnotcontainany0initsdecimalrepresentation.Givenani......