博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
94. Binary Tree Inorder Traversal
阅读量:5157 次
发布时间:2019-06-13

本文共 4057 字,大约阅读时间需要 13 分钟。

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

Example:

Input: [1,null,2,3]   1    \     2    /   3Output: [1,3,2]

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

 

Approach #1: recurisive.

/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    vector
inorderTraversal(TreeNode* root) { vector
ans; helper(root, ans); return ans; } private: void helper(TreeNode* root, vector
& ans) { if (root != NULL) { if (root->left != NULL) { helper(root->left, ans); } ans.push_back(root->val); if (root->right != NULL) { helper(root->right, ans); } } }};

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Binary Tree Inorder Traversal.

 

Approach #2: Iteration with stack.[Java]

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */class Solution {    public List
inorderTraversal(TreeNode root) { List
res = new ArrayList< > (); Stack
stack = new Stack< > (); TreeNode curr = root; while (curr != null || !stack.isEmpty()) { while (curr != null) { stack.push(curr); curr = curr.left; } curr = stack.pop(); res.add(curr.val); curr = curr.right; } return res; }}
Runtime: 
1 ms, faster than 60.30% of Java online submissions for Binary Tree Inorder Traversal.

 

Approach #3: Morris Traversal.[python] 

# Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass Solution(object):    def inorderTraversal(self, root):        """        :type root: TreeNode        :rtype: List[int]        """        res = []        curr = root        while curr:            if not curr.left:                res.append(curr.val)                curr = curr.right            else:                pre = curr.left                while pre.right:                    pre = pre.right                pre.right = curr                temp = curr                curr = curr.left                temp.left = None        return res

Runtime: 24 ms, faster than 40.09% of Python online submissions for Binary Tree Inorder Traversal.

 

 

转载于:https://www.cnblogs.com/ruruozhenhao/p/9943581.html

你可能感兴趣的文章
堆排序的Python实现
查看>>
一起学爬虫——如何爬取通过ajax加载数据的网站
查看>>
spring事务分类简述
查看>>
nginx 配置反向代理和负载均衡
查看>>
劝学-荀子
查看>>
第十次作业
查看>>
ZOJ1586——QS Network(最小生成树)
查看>>
HDU5087——Revenge of LIS II(BestCoder Round #16)
查看>>
管理信息系统 课程设计
查看>>
Spring boot与Quartz实现任务定时提醒
查看>>
python基础-函数之装饰器、迭代器与生成器
查看>>
BZOJ 1878 hh的项链(简单莫队)
查看>>
使用OctreeQuantizer提高gdi+绘图质量
查看>>
Asset Store 下载的package存在什么地方?
查看>>
[凯立德]2015春季版C2739-M7L83-3521JON,已O+带3D+带路况
查看>>
Linux实战教学笔记32:企业级Memcached服务应用实践
查看>>
php-day1
查看>>
820
查看>>
C/C++面试之算法系列--去除数组中的重复数字
查看>>
python插入排序算法总结
查看>>