数据结构--二叉树
exmy
Less is more, and more is less.
展开
-
PAT--1119. Pre- and Post-order Traversals
1119. Pre- and Post-order Traversals题解reference#include <iostream>#include <cstdio>#include <algorithm>#include <vector>using namespace std;const int maxn = 35;int pre[maxn], post[maxn];int n;ve原创 2016-10-27 23:36:09 · 350 阅读 · 0 评论 -
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).题解一个NULL标记一层的结束。/** * Definition for a binary tree node. * struct TreeNode { *原创 2017-01-25 21:56:49 · 258 阅读 · 0 评论 -
leetcode--104. 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 root node down to the farthest leaf node.题解/** * Definition for a binary tree node.原创 2017-01-26 15:56:40 · 249 阅读 · 0 评论 -
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).题解/** * Definition for a binary tree node. * public cl原创 2017-02-03 23:30:40 · 313 阅读 · 0 评论 -
PAT--1127. ZigZagging on a Tree
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standa原创 2017-03-05 23:59:47 · 1231 阅读 · 0 评论 -
PAT--1123. Is It a Complete AVL Tree (30)
link题解AVL树的插入,然后判断一下是否为完全二叉树。 题目简单,然而考试的时候没做出来= = 好久没写过平衡树,记不得了,只写了个普通的BST得了可怜的6分。。 基础不牢啊。。务实基础!!!#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <queue>#inclu原创 2016-12-10 21:26:21 · 736 阅读 · 2 评论 -
PAT--1130. Infix Expression(中序加括号)
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with parentheses reflecting the precedences of the operators.Input Specification:Each input file contains on原创 2017-03-25 18:46:30 · 584 阅读 · 0 评论 -
PAT--1064. Complete Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with keys less than the node’s key. The right subtr原创 2017-07-26 23:12:12 · 323 阅读 · 0 评论 -
leetcode--101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center)./** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; *原创 2017-01-25 20:14:13 · 248 阅读 · 0 评论 -
leetcode--100. Same Tree
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.题解/** * Definition原创 2017-01-25 18:51:11 · 238 阅读 · 0 评论 -
leetcode--112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.题解class Solution {public: bool hasPathSum(TreeNo原创 2017-02-10 21:18:10 · 275 阅读 · 0 评论 -
UVa--548 Tree(二叉树)
UVa 548题解根据中序后序序列直接建立(lch[], rch[]),表示结点的左右子节点的权值。#include <iostream>#include <cstdio>#include <string>#include <sstream>#include <climits>#include <algorithm>using namespace std;const int maxn =原创 2016-07-14 20:02:57 · 516 阅读 · 0 评论 -
PAT--1115. Counting Nodes in a BST
pat A.1115题解每个结点附加一个域表示该结点所在的层次,建树的时候同时计算。 然后遍历之。#include <iostream>#include <string>#include <algorithm>using namespace std;typedef struct node* BinTree;struct node{ int k; int l; Bin原创 2016-03-16 19:51:24 · 501 阅读 · 0 评论 -
PAT--1110. Complete Binary Tree
link to problem题解主要是构建树,用数组来存储的话很容易判断是不是完全二叉树。找出根节点然后递归构造。#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cstdlib>using namespace std;const int maxn = 100;st原创 2016-10-30 21:00:24 · 299 阅读 · 0 评论 -
PAT--1102. Invert a Binary Tree
problem link题解反转一棵二叉树,然后层序遍历和中序遍历。trick: 输入的时候左右节点互换,就实现了反转。#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <queue>#include <algorithm>#include <vector>using name原创 2016-11-20 00:01:35 · 294 阅读 · 0 评论 -
PAT--1099. Build A Binary Search Tree
link题解根据给定的二叉搜索树和插入这棵树的整数值,求其层序遍历。中序遍历二叉搜索树可以得到有序的序列,此题正好相反,将原有序列排好序后中序插入之。#include <iostream>#include <algorithm>#include <vector>#include <cstdio>#include <queue>using namespace std;// remain原创 2016-12-09 12:00:48 · 315 阅读 · 0 评论 -
leetcode--108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.题解以序列的中间元素作为根,转换后的BST肯定是平衡的。/** * Definition for a binary tree node. * struct TreeNode { * int va原创 2017-02-08 21:45:46 · 259 阅读 · 0 评论 -
leetcode--110. 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 binary tree in which the depth of the two subtrees of every node never differ by原创 2017-02-09 22:24:43 · 233 阅读 · 0 评论 -
leetcode--111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.题解和求最大深度类似,不过求最小深度需要注意的是,当一个节点只有左子树或者右子树的时候原创 2017-02-10 21:03:11 · 255 阅读 · 0 评论 -
PAT--1086. Tree Traversals Again
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack原创 2017-09-05 00:18:08 · 342 阅读 · 0 评论