Leetcode - Tree - Binary Tree Traversal

智能驾驶感知模块的测试评估方法(上) 对于智能驾驶系统而言,感知模块是开发难度最大、投入资源最多的核心部分。在智能驾驶开发团队内部,感知模块需要独立进行评测才能及早发现潜在缺陷或判断性能是否有改进。随着近几年BEV、OCC、E2E等新技术方案被越来越多企业应用,感知的评测变得越来越艰难,效率也变得越来越低下。本文作者基于在智驾测试行业多年经验,试图总结感知评测的基本规律和效率提升方法,以供读者一些启发。 阅读详情

1、Binary Tree Preorder Traversal 

Given a binary tree, return the preorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3

return [1,2,3].

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

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode *root) {
        vector<int> nodeValue;
        preorder(root, nodeValue);
        return nodeValue;
    }
    void preorder(TreeNode *root, vector<int> &nodeValue)
    {
        if(root==NULL)
            return;
        nodeValue.push_back(root->val);
        preorder(root->left, nodeValue);
        preorder(root->right, nodeValue);
    }
};
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode *root) {
        // 时间复杂度为O(n),空间复杂度为O(h)
        vector<int> nodeValue;
        if(root==NULL)
            return nodeValue;
        stack<TreeNode*> nodeStack;
        nodeStack.push(root);
        while(!nodeStack.empty())
        {
            root=nodeStack.top();
            nodeStack.pop();
            nodeValue.push_back(root->val);
            if(root->right!=NULL)
                nodeStack.push(root->right);
            if(root->left!=NULL)
                nodeStack.push(root->left);
        }
        return nodeValue;
    }
};


2、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?

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode *root) {
        vector<int> nodeValue;
        inorder(root, nodeValue);
        return nodeValue;
    }
    void inorder(TreeNode *root, vector<int> &nodeValue)
    {
        if(root==NULL)
            return;
        inorder(root->left,nodeValue);
        nodeValue.push_back(root->val);
        inorder(root->right,nodeValue);
    }
};
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode *root) {
        vector<int> nodeValue;
        stack<TreeNode*> nodeStack;
        while(root!=NULL||!nodeStack.empty())
        {
            //运行到这儿,root不允许为之前出现过的结点,否则死循环
            while(root!=NULL)
            {
                nodeStack.push(root);
                root=root->left;
            }
            if(!nodeStack.empty())
            {
                root=nodeStack.top();
                nodeValue.push_back(root->val);
                nodeStack.pop();
                root=root->right;
            }
        }
        return nodeValue;
    }
};

3、Binary Tree Postorder Traversal 

Given a binary tree, return the postorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3

return [3,2,1].

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

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode *root) {
        vector<int> nodeValue;
        postorder(root, nodeValue);
        return nodeValue;
    }
    void postorder(TreeNode *root, vector<int> &nodeValue)
    {
        if(root==NULL)
            return;
        postorder(root->left, nodeValue);
        postorder(root->right, nodeValue);
        nodeValue.push_back(root->val);
    }
};

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode *root) {
        vector<int> nodeValue;
        stack<TreeNode*> nodeStack;
        if(root!=NULL)
        {
            nodeStack.push(root);
            while(!nodeStack.empty())
            {
                root=nodeStack.top();
                nodeStack.pop();
                nodeValue.push_back(root->val);
                if(root->left!=NULL) nodeStack.push(root->left);
                if(root->right!=NULL) nodeStack.push(root->right);
            }
            reverse(nodeValue.begin(), nodeValue.end());
        }
        return nodeValue;
    }
};

4、Binary Tree Level Order Traversal 

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

For example:
Given binary tree {3,9,20,#,#,15,7},

    3
   / \
  9  20
    /  \
   15   7

return its level order traversal as:

[
  [3],
  [9,20],
  [15,7]
]
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int> > levelOrder(TreeNode *root) {
        vector<vector<int> > path;
        vector<int> levelPath;
        if(root==NULL)
            return path;
        queue<TreeNode*> node_handling, node_waiting;
        node_waiting.push(root);
        while(!node_waiting.empty())
        {
            swap(node_handling,node_waiting);
            while(!node_handling.empty())
            {
                root=node_handling.front();
                node_handling.pop();
                levelPath.push_back(root->val);
                if(root->left!=NULL)node_waiting.push(root->left);
                if(root->right!=NULL)node_waiting.push(root->right);
            }
            path.push_back(levelPath);
            levelPath.clear();
        }
        return path;
    }
};


5、Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree {3,9,20,#,#,15,7},

    3
   / \
  9  20
    /  \
   15   7

return its bottom-up level order traversal as:

[
  [15,7],
  [9,20],
  [3]
]
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int> > levelOrderBottom(TreeNode *root) {
        vector<vector<int>> path;
        int depth=0;
        nLevelOrder(root, depth, path);
        reverse(path.begin(), path.end());
        return path;
    }
    void nLevelOrder(TreeNode *root, int depth, vector<vector<int>> &path)
    {
        if(root!=NULL)
        {
            if(depth>=path.size())
            {
                vector<int> temp{root->val};
                path.push_back(temp);
            }
            else
                path[depth].push_back(root->val);
            nLevelOrder(root->left, depth+1, path);
            nLevelOrder(root->right, depth+1, path);
        }
    }
};
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int> > levelOrderBottom(TreeNode *root) {
        vector<vector<int>> path;
        vector<int> levelPath;
        if(root!=NULL)
        {
            queue<TreeNode*> node_handling, node_waiting;
            node_waiting.push(root);
            while(!node_waiting.empty())
            {
                swap(node_handling,node_waiting);
                while(!node_handling.empty())
                {
                    root=node_handling.front();
                    node_handling.pop();
                    levelPath.push_back(root->val);
                    if(root->left) node_waiting.push(root->left);
                    if(root->right) node_waiting.push(root->right);
                }
                
                path.push_back(levelPath);
                levelPath.clear();
            }
            reverse(path.begin(),path.end());
        }
        return path;
    }
};


6、Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).

For example:
Given binary tree {3,9,20,#,#,15,7},

    3
   / \
  9  20
    /  \
   15   7

return its zigzag level order traversal as:

[
  [3],
  [20,9],
  [15,7]
]
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
        vector<vector<int>> path;
        vector<int> levelPath;
        if(root!=NULL)
        {
            bool zigzag=false;
            queue<TreeNode*> node_handling, node_waiting;
            node_waiting.push(root);
            while(!node_waiting.empty())
            {
                swap(node_handling, node_waiting);
                while(!node_handling.empty())
                {
                    root=node_handling.front();
                    node_handling.pop();
                    levelPath.push_back(root->val);
                    if(root->left)node_waiting.push(root->left);
                    if(root->right)node_waiting.push(root->right);
                }
                if(zigzag)
                {
                    reverse(levelPath.begin(), levelPath.end());
                    zigzag=false;
                }
                else
                    zigzag=true;
                path.push_back(levelPath);
                levelPath.clear();
            }
        }
        return path;
    }
};






gdb对dwarf调试信息的解析和使用 gdb的对dwarf的调试信息的解析到使用的过程分析 阅读详情

相关推荐

PX4笔记系列(二)vtol_att_control中的tiltrotor解析

update_vtol_state() 注释:采用两段切换的简单逻辑来进行过渡 当切换过渡开关后,飞机将会开始倾转动力,逐渐加速,当飞机获得足够速度后动力将会完全打到向前的位置。对于过渡返回阶段动力则是简单地倾转回来。 如果(不在向前过渡指令下): 当前vtol飞行模式: 1、旋翼模式 无操作 2、固定翼模式 令vtol飞行模式切换到过渡返回模式 3、P1过渡模式 进入过渡失败安全返回至旋翼模式 4、P2过渡模式 进入过渡失败安全返回至旋翼模式 5、过渡返回模式 记录过渡返回时间=当前时间-过渡开始

hypersonicss的博客 1880

【LeetCode】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.youkuaiyun.com/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, return the zigzag level ord

crazy_jack 9869

Elasticsearch 8.17.1 JAVA工具类

整理了Elasticsearch8.17最新版的API工具类

weixin_39555954的博客 1125

LeetCode力扣之102. Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \...

LeeWei 209

无人机WIFI集群组网技术详解及成本分析

无人机WIFI集群组网技术是指利用无人机作为移动平台,通过集成高性能的WIFI模块,实现多架无人机之间以及无人机与地面控制站之间的无线通信组网。在协议层面,无人机WIFI集群组网需要遵循标准的无线通信协议,如IEEE 802.11系列(即WIFI标准),同时可能还需定制或优化路由协议、媒体访问控制(MAC)协议等,以确保高效、可靠的数据传输。数据安全与隐私保护:无人机集群组网中传输的数据可能涉及敏感信息,需采取加密、认证等安全措施,确保数据传输的安全性和用户隐私的保护。根据性能不同,价格差异较大。

专注多旋翼、复合翼无人机技术研究,从事低空经济行业。 1583

工业质检新利器:手把手教你用AD-DINOv3实现零样本缺陷检测(附MVTec AD实战)

本文详细介绍了AD-DINOv3框架在工业质检中的应用,通过零样本异常检测技术解决传统方法的标注成本高和泛化能力弱问题。文章包含技术原理解析、MVTec AD数据集实战部署、模型训练流程及工业部署优化技巧,帮助读者快速掌握这一创新技术。

seed的博客 764

Leetcode学习笔记:#1008. Construct Binary Search Tree from Preorder Traversal

Leetcode学习笔记:#1008. Construct Binary Search Tree from Preorder Traversal Return the root node of a binary search tree that matches the given preorder traversal. (Recall that a binary search tree is a ...

ccystewart的博客 231

PaddleOCR 与 PaddleX 调试

本文介绍了PaddleOCR与PaddleX的安装调试过程及应用对比。首先详细说明了环境准备步骤,包括安装ccache、设置环境变量及创建conda虚拟环境,提供了精简安装和源码安装两种方式。接着展示了使用PaddleOCR进行发票和手写汉字识别的测试结果,指出部分手写识别存在误差。随后对比了两者的差异:PaddleOCR专注于OCR算法优化,PaddleX则提供全流程工具链和标准化部署方案。最后演示了如何通过PaddleX部署OCR服务,包括API启动方法和客户端调用示例,为开发者提供了两种不同场景下的O

weixin_42176639的博客 1452

LeetCode-94. Binary Tree Inorder Traversal [C++][Java]

Given therootof a binary tree, returnthe inorder traversal of its nodes' values.

贫道绝缘子的博客 226

关于卷积过程中通道数如何变化问题

对卷积操作前后通道数问题进行解释,输出通道数=卷积核的个数。

你好,明天,,的博客 4316

COSMO-RS理论:流体相热力学预测的核心模型与技术突破

摘要:COSMO-RS模型通过量子化学计算分子表面电荷分布,实现了从微观量子尺度到宏观化工过程的热力学预测。该模型基于介电连续介质理论,将分子表面片段化处理,通过σ-剖面描述电荷分布,并计算片段间的静电失配能和氢键能。其核心算法通过自洽化学势方程求解活度系数,克服了传统模型依赖实验参数的局限。随着技术进步,模型引入了高阶描述符和色散力修正,提升了预测精度。在药物研发、化工分离等领域广泛应用,但仍存在对长程有序结构预测不足等局限性。该模型为复杂体系的热力学性质预测提供了重要理论工具。

ASKCOS博客 973

LeetCode - 107. Binary Tree Level Order Traversal II

题目 LeetCode - 107. Binary Tree Level Order Traversal II 题目链接 https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ 参考博客 解题思路 此题是题102的变体,略过。 解题源码 /** * Definition for a binary tree node....

virgilshi的博客 158

prplOS amx-fxgi调试-订阅subscribe

本文分析了prplOS中amx-fxgi订阅功能(subscribe)的实现与调试过程。通过梳理订阅流程发现,默认订阅失败主要由于acl规则限制导致。文章详细介绍了订阅的两个关键步骤:建立GET长连接和POST设置订阅路径,并通过修改代码绕过acl验证成功实现订阅功能。最终验证了在同一连接下订阅多个路径及实时接收状态更新的可行性,有效降低了设备查询负载。该方案为prplOS的实时数据监控提供了优化思路。

四儿家的小祖宗的博客 330

LeetCode力扣之103. Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tree [...

LeeWei 223

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Binary Tree Zigzag Level Order Traversal - LeetCode

Binary Tree Zigzag Level Order Traversal - LeetCode 题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the nex

不要说话的博客 569

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

[LeetCode] Binary Tree Inorder Traversal

[Problem] 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 triv

1.02^365的成长裂变 3705

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

LeetCode 107. Binary Tree Level Order Traversal II

LeetCode 107. Binary Tree Level Order Traversal II Solution1:我的答案 比102那道题多了一行代码。。。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeN...

Allenlzcoder的博客 286

LeetCode 力扣 145. 二叉树的后序遍历 binary tree postorder traversal

大家觉得写还可以,可以点赞、收藏、关注一下吧! 也可以到我的个人博客参观一下,估计近几年都会一直更新!和我做个朋友吧!https://motongxue.cn 文章目录145. 二叉树的后序遍历题目描述示例:进阶分析递归版迭代版提交结果 145. 二叉树的后序遍历 题目描述 给定一个二叉树,返回它的 后序 遍历。 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶 递归算法很简单,你可以通过迭代算法完成吗? 分析 递归

motongxue的博客 192

Leetcode: Binary Tree Level Order Traversal II

题目 https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 分析 和

博客已迁移至 http://chenzz.me 711

LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal

原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题目: Return any binary tree that matches the given preorder and postorder traversals. Values in the...

weixin_30888027的博客 92

LeetCode Binary Tree Inorder Traversal

不用递归来实现树的中序遍历。

DRFish 1392

[勇者闯LeetCode] 107. Binary Tree Level Order Traversal II

[勇者闯LeetCode] 107. Binary Tree Level Order Traversal IIDescription Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from l

fanguzhai的博客 232

LeetCode—103. Binary Tree Zigzag Level Order Traversal

LeetCode—103. Binary Tree Zigzag Level Order Traversal 题目 https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/ 之字形层序遍历二叉树。 思路及解法 因为昨天刚做过层序遍历二叉树的题102. Binary Tree Level ...

pnnngchg的博客 377

Binary Tree Preorder Traversal leetcode

Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3]. 二叉树的前序遍历,非递归的写法考的比较多,方法很

shiralwz的专栏 314

LeetCode力扣之107. Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree [3,9,20,null,null,15,7], ...

LeeWei 216

Leetcode 145. Binary Tree Postorder Traversal

Leetcode 145. Binary Tree Postorder Traversal 问题描述二叉树的遍历后根序遍历,也就是左右根的顺序访问。 分别提供以下方法以及python代码: 递归的方式 class Solution: def postorderTraversal(self, root): """ :type root: TreeNode ...

Za七杂⑧ 335
上一篇: Leetcode - Tree /Dynamic Programming- Unique Binary Search
下一篇: Leetcode - Tree - Balanced Binary Tree
JerkWisdom
博客等级 码龄14年 70粉丝 76原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值