Binary Tree Inorder Traversal
来源:互联网 发布:人工智能测试工程师 编辑:程序博客网 时间:2024/05/14 08:28
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].
class Solution {public: vector<int> inorderTraversal(TreeNode* root) { vector<int> result; if(root != NULL) //recursiveInorderTraversal(root, result); return result; }private: /* void recursiveInorderTraversal(TreeNode* root, vector<int> &result) { if(root == NULL) return; if(root->left != NULL) recursiveInorderTraversal(root->left, result); result.push_back(root->val); if(root->right != NULL) recursiveInorderTraversal(root->right, result); } */ void iterativelInorderTraversal(TreeNode* root, vector<int> &result) { stack<TreeNode *> leftSubTree; TreeNode *p = root; while(p != NULL || !leftSubTree.empty()) { while(p != NULL) //将左子树一直入栈 { leftSubTree.push(p); p = p->left; } if(!leftSubTree.empty()) { p = leftSubTree.top(); result.push_back(p->val); leftSubTree.pop(); p = p->right; //开始遍历右子树 } } }};
0 0
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- 第二章JavaScript对象
- 设备树学习之(二)点灯
- dubbo组成原理-zookeeper注册原理
- C# 结构体与类的区别
- linux查看文件20到30行的三种方法
- Binary Tree Inorder Traversal
- MySQL Replication 的基本知识及简单配置(1)
- 如何读取assets下的文件
- [leetcode]: 349. Intersection of Two Arrays
- boost::function
- js判断当前浏览类型
- Data Structures and Algorithm Analysis in C, Second Edition(《数据结构与算法分析》C语言版 第二版)——Mark Allen Weiss
- poj1664——放苹果(递推)
- swap.c -- 使用指针解决交换函数的问题