二叉树的层次遍历(BFS),二叉树的所有路径,二叉树的最大路径和(分治)
2017-04-11 17:04
447 查看
给一棵二叉树 {3,9,20,#,#,15,7} :
3
/ \
9 20
. . / \
..15 7
返回他的分层遍历结果:
[
[3],
[9,20],
[15,7]
]
给一棵二叉树,找出从根节点到叶子节点的所有路径。
1
/ \
2 3
\
5
所有根到叶子的路径为:
[
“1->2->5”,
“1->3”
]
给出一棵二叉树,寻找一条路径使其路径和最大,路径可以在任一节点中开始和结束(路径和为两个节点之间所在路径上的节点权值之和)
1
/ \
2 3
return 6
3
/ \
9 20
. . / \
..15 7
返回他的分层遍历结果:
[
[3],
[9,20],
[15,7]
]
class Solution { /** * @param root: The root of binary tree. * @return: Level order a list of lists of integer */ public: vector<vector<int>> levelOrder(TreeNode *root) { // write your code here vector<vector<int> >res; if (root == NULL) { return res; } queue<TreeNode* >q; //申请队列 q.push(root); while(!q.empty()){ int size = q.size();//每次循环size次 vector<int> level; for (int i = 0; i< size; i++){ TreeNode* top = q.front(); q.pop(); level.push_back(top->val); if(top->left != NULL) q.push(top->left); if(top->right != NULL) q.push(top->right); } res.push_back(level); } return res; } };
给一棵二叉树,找出从根节点到叶子节点的所有路径。
1
/ \
2 3
\
5
所有根到叶子的路径为:
[
“1->2->5”,
“1->3”
]
/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */ class Solution { public: /** * @param root the root of the binary tree * @return all root-to-leaf paths */ void helper(TreeNode* root, string str, vector<string>& res){ if (root == NULL) { return ; } else if (root != NULL && root->left == NULL && root->right == NULL){ str += to_string(root->val); res.push_back(str); return ; } str += to_string(root->val); if (root->left != NULL){ string temp = str + "->"; helper(root->left, temp, res); } if (root->right != NULL){ string temp = str + "->"; helper(root->right, temp, res); } return ; } vector<string> binaryTreePaths(TreeNode* root) { // Write your code here string str; vector<string> res; helper(root, str, res); return res; } };
给出一棵二叉树,寻找一条路径使其路径和最大,路径可以在任一节点中开始和结束(路径和为两个节点之间所在路径上的节点权值之和)
1
/ \
2 3
return 6
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int ans = INT_MIN; int maxPathSum(TreeNode *root) { maxPath(root); return ans; } int maxPath(TreeNode* root){ if (root == NULL) return 0; int left = 0,right = 0; if (root ->left) left = maxPath(root ->left); if (root ->right) right = maxPath(root ->right); int sum = root ->val + max(0, left) + max(0, right); ans = max (ans, sum); return root ->val + max(0, max(left, right)); } };
相关文章推荐
- 二叉树建立 以及 bfs层次遍历
- 面试常考手写代码之--二叉树层次遍历(BFS)
- 遍历二叉树所有路径并求指定值
- LintCode-分治-二叉树中的最大路径和
- 面试常考手写代码之--二叉树层次遍历(BFS)
- 层次遍历之寻找二叉树每层最大值
- 数据结构—树和二叉树-2.二叉树的层次遍历(BFS)
- leetcode 515. Find Largest Value in Each Tree Row 二叉树每一层最大值+广度优先遍历BFS
- Java实现二叉树的先序、中序、后序、层次遍历,数的最大深度、最大宽度
- 6-3-2 二叉树层次遍历(BFS)
- 二叉树中相差最大的两个节点间的差的绝对值(层次遍历+递归先序)
- 二叉树的构建、层次打印、广度遍历、深度遍历、K值路径
- 二叉树的层次,中序非递归遍历,以递归前序的方式构造二叉树,将二叉树中的e更新为d,输出从根结点出发 到指定结点,依次经过的祖先(即路径),由前序和中序还原二叉树
- 程序员面试100题(算法)之二叉树中找出和为某一值的所有路径(含二叉树前序创建、遍历)
- 1501 二叉树最大宽度和高度 (BFS+树的遍历)
- 初始BFS ,以及二叉树层次遍历
- 遍历整个二叉树的所有路径
- 二叉树系列二:二叉树的层次遍历(BFS)
- (1)建立二叉树的二叉链表。 (2)写出对用二叉链表存储的二叉树进行先序、中序和后序遍历的递归和非递归算法。 (3)写出对用二叉链表存储的二叉树进行层次遍历算法。 (4)求二叉树的所有叶子及结点总数。
- hdoj 3468 Treasure Hunting 【BFS找所有最短路径上的点 + 最大流】