二叉树先序遍历——144. Binary Tree Preorder Traversal
来源:互联网 发布:螺钿鱼骨项链淘宝网 编辑:程序博客网 时间:2024/05/21 09:36
144. Binary Tree Preorder Traversal
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
Subscribe to see which companies asked this question.
/** * https://leetcode.com/problems/binary-tree-preorder-traversal/#/description * * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } *//** * @param {TreeNode} root * @return {number[]} *//** * 先序遍历非递归 * 1.申请一个stack,如果根节点存在,将根节点push到stack中 * 2.从stackpop一个节点,执行操作后,如果右边还是存在push到stack中,如果左边还是存在push到stack中 * 3.重复2直到stack为空 */ var preorderTraversal = function(root) { var stack = [], node, result = []; if (!root) return result; stack.push(root); while (stack.length) { node = stack.pop(); result.push(node.val); if (node.right) stack.push(node.right); if (node.left) stack.push(node.left); } return result; };
阅读全文
0 0
- 二叉树先序遍历——144. Binary Tree Preorder Traversal
- LeetCode144——Binary Tree Preorder Traversal二叉树先序遍历
- 【二叉树先序遍历】Binary Tree Preorder Traversal
- 144.Binary Tree Preorder Traversal(二叉树先序遍历)
- 144.leetcode Binary Tree Preorder Traversal(medium)[二叉树先序遍历]
- [leetcode-二叉树先序遍历]--144.Binary Tree Preorder Traversal
- LeetCode Binary Tree Preorder Traversal (二叉树先序遍历非递归)
- Leetcode Binary Tree Preorder Traversal 二叉树先序遍历
- 二叉树遍历144. Binary Tree Preorder Traversal
- [LeetCode]144. Binary Tree Preorder Traversal--二叉树前序遍历
- leetcode 144. Binary Tree Preorder Traversal 二叉树前序遍历 + DFS
- 【LeetCode】144. Binary Tree Preorder Traversal 二叉树先序遍历的非递归实现
- 5.1.1—二叉树的遍历—Binary Tree Preorder Traversal
- Algorithms—144.Binary Tree Preorder Traversal
- 二叉树的先序遍历 Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal--二叉树的先序遍历
- Binary Tree Preorder Traversal 二叉树的前序遍历
- Leetcode: Binary Tree Preorder Traversal(二叉树前序遍历)
- Android 控件布局
- heartbeat实现HA
- Mysql计数器表设计
- 【原】判断点是否在多变形内(matlab版)
- 遇到@RunWith(SpringJUnit4ClassRunner.class)报错 解决方案
- 二叉树先序遍历——144. Binary Tree Preorder Traversal
- <纯干货-4> 加州伯克利大学2017年最新深度强化学习视频课程_part1
- 网络安全法将在六一正式实施,我该如何继续爱你?
- VMWARE 占用硬盘空间越来越大的解决方法
- win7 64位安装VS2013配置openCV3.1
- QLineEdit设置错误提示信息,并且设置QLineEdit设置为红色
- C语言模拟栈存储结构笔记
- Python定义函数及引用
- Pyhon内置的数据类型详解