二叉树
ych_ding
这个作者很懒,什么都没留下…
展开
-
leetcode 二叉树安装层次进行遍历
问题描述:对于二叉树root按照level进行访问,其中可以有几个变种: 每层从左到右、从右到左、zig-zag order。问题分析:伪代码:原创 2014-12-19 09:55:57 · 828 阅读 · 0 评论 -
计算二叉树的高度中序迭代版本,有错误待定位
/** * 计算二叉树的高度 * 根节点的高度为1,空节点的高度0 * 采用中序迭代的方式进行求解 * * 树的高度就是运行时栈的最大深度 * 该想法经过验证有问题,对于没有左 * 树的情况不对。 * **/ int height_in_iterative(node *root) { if (!root) return 0; int h = 0; int原创 2014-07-16 09:51:11 · 469 阅读 · 0 评论 -
leetcode binary tree maximum path sum
问题描述:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/点击打开链接Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example原创 2014-11-29 18:48:29 · 423 阅读 · 0 评论 -
判定二叉树是否是BST 递归版本
/* 判定二叉树是否是BST * BST * 注意体会第二参数的意义:前一个被访问节点的值,需要使用引用 * * 时间复杂度O(n),需要逐个遍历树中的元素。 * 思路: bst的中序序列是严格递增的 * */ bool is_bst_by_in(node *root, int& preval) { if (!root) return true; /原创 2014-07-16 10:10:20 · 637 阅读 · 0 评论 -
计算二叉树的高度递归版本
/** * 计算二叉树的高度 * 根节点的高度为1,空节点的高度0 * 采用递归的方式进行求解 * **/ int height(node *root) { int h = 0; if (!root) return 0; int h1 = height(root->l); int h2 = height(root->r); if (h1原创 2014-07-16 09:47:39 · 1714 阅读 · 0 评论 -
二叉树中序遍历的迭代版本
/*** 二叉树遍历的中序的迭代版本 * 需要stack 结构, cur变量的帮助 * 每个节点都作为左子树被压入栈,作为根被访问, * 再转入右子树做重复的处理 * ***/ void in_visit_iterative(node *root) { if (!root) return; node *cur = root; sta原创 2014-07-16 09:49:37 · 2548 阅读 · 0 评论 -
二叉树深度计算
问题描述https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/点击打开链接Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the r原创 2014-12-10 22:31:10 · 19752 阅读 · 0 评论 -
bst的serialize和unserialize
void serialize(node *root, ofstream &file) { if (!root) return; file dat << " "; serialize(root->l, file); serialize(root->r, file); } node * unserialize(ifstream &file) { node * r原创 2014-07-16 15:35:27 · 562 阅读 · 0 评论 -
leetcode 平衡二叉树判断
问题描述:https://oj.leetcode.com/problems/balanced-binary-tree/点击打开链接Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binar原创 2014-12-10 22:49:29 · 1941 阅读 · 0 评论 -
二叉树中节点的最大距离
问题描述:问题分析:示例代码:int maxDistanc(TreeNode *root, int & height){if (!root || (!root->left && !root->right)){height = 0; return 0;}int hl, hr, maxleft, maxright;maxleft = maxDistanc原创 2015-01-12 21:11:36 · 380 阅读 · 0 评论 -
leetcode convert-sorted-array-to-binary-search-tree
问题描述:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.https://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/点击打开链接问题分析:原创 2014-12-20 11:54:37 · 540 阅读 · 0 评论 -
Binary tree related algorithms summary
validate a binary tree is BST思路: bst的中序遍历序列是严格递增的。 1. 空树是BST 2. 左子树是BST && 右子树是BST 3. 左子树 && 根 && 右子树 能构成BST. sample code /* 判定二叉树是否是BST * 第二参数的意义:前一个被访问节点的值,需要使用引用 * 时间复杂度O(n),需要逐个遍原创 2016-02-03 22:26:08 · 534 阅读 · 0 评论