leetcode 655. Print Binary Tree 深度优先遍历DFS
来源:互联网 发布:java微信公众开发项目 编辑:程序博客网 时间:2024/06/05 01:52
Print a binary tree in an m*n 2D string array following these rules:
The row number m should be equal to the height of the given binary tree.
The column number n should always be an odd number.
The root node’s value (in string format) should be put in the exactly middle of the first row it can be put. The column and the row where the root node belongs will separate the rest space into two parts (left-bottom part and right-bottom part). You should print the left subtree in the left-bottom part and print the right subtree in the right-bottom part. The left-bottom part and the right-bottom part should have the same size. Even if one subtree is none while the other is not, you don’t need to print anything for the none subtree but still need to leave the space as large as that for the other subtree. However, if two subtrees are none, then you don’t need to leave space for both of them.
Each unused space should contain an empty string “”.
Print the subtrees following the same rules.
Example 1:
Input:
1
/
2
Output:
[[“”, “1”, “”],
[“2”, “”, “”]]
Example 2:
Input:
1
/ \
2 3
\
4
Output:
[[“”, “”, “”, “1”, “”, “”, “”],
[“”, “2”, “”, “”, “”, “3”, “”],
[“”, “”, “4”, “”, “”, “”, “”]]
Example 3:
Input:
1
/ \
2 5
/
3
/
4
Output:
[[“”, “”, “”, “”, “”, “”, “”, “1”, “”, “”, “”, “”, “”, “”, “”]
[“”, “”, “”, “2”, “”, “”, “”, “”, “”, “”, “”, “5”, “”, “”, “”]
[“”, “3”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”]
[“4”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”]]
Note: The height of binary tree is in the range of [1, 10].
我们先来看第一行,由于根结点只有一个,所以第一行只需要插入一个数字,不管这一行多少个位置,我们都是在最中间的位置插入结点值。下面来看第二行,我们仔细观察可以发现,如果我们将这一行分为左右两部分,那么插入的位置还是在每一部分的中间位置,这样我们只要能确定分成的部分的左右边界位置,就知道插入结点的位置了,所以应该是使用分治法的思路。在递归函数中,如果当前node不存在或者当前深度超过了最大深度直接返回,否则就给中间位置赋值为结点值,然后对于左子结点,范围是左边界到中间位置,调用递归函数,注意当前深度加1;同理对于右子结点,范围是中间位置加1到右边界,调用递归函数,注意当前深度加1,
代码如下:
#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>#include <functional>#include <bitset>#include <numeric>#include <cmath>#include <regex>using namespace std;/*struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};*/class Solution {public: vector<vector<string>> printTree(TreeNode* root) { int h = getHeight(root), w = pow(2, h) - 1; vector<vector<string>> res(h,vector<string>(w,"")); dfs(root, 0, 0, w - 1, res); return res; } void dfs(TreeNode* root, int depth , int begin, int end, vector<vector<string>>& res) { if (root == NULL) return; else { int mid = (begin + end) / 2; res[depth][mid] = to_string(root->val); dfs(root->left, depth + 1, begin, mid, res); dfs(root->right, depth + 1, mid+1, end, res); } } int getHeight(TreeNode* root) { if (root == NULL) return 0; else { int left = getHeight(root->left) + 1; int right = getHeight(root->right) + 1; return max(left,right); } }};
- leetcode 655. Print Binary Tree 深度优先遍历DFS
- leetcode 257. Binary Tree Paths 深度优先遍历DFS
- leetcode 606. Construct String from Binary Tree 前序遍历 + 深度优先遍历DFS
- leetcode 110. Balanced Binary Tree(平衡二叉树的判断) DFS深度优先遍历
- leetcode 111. Minimum Depth of Binary Tree DFS深度优先遍历 + 添加对叶子节点判断
- leetcode 543. Diameter of Binary Tree 最长树的片段 + 深度优先遍历DFS
- leetcode 671. Second Minimum Node In a Binary Tree 第二小数字 + 深度优先遍历DFS
- leetcode 669. Trim a Binary Search Tree 修建二叉搜索树BST + 深度优先遍历DFS
- leetcode 662. Maximum Width of Binary Tree 深度优先遍历DFS
- leetcode 617. Merge Two Binary Trees 深度优先遍历DFS
- leetcode 100. Same Tree 二叉树DFS深度优先遍历
- leetcode 572. Subtree of Another Tree 深度优先遍历DFS
- Leetcode--655. Print Binary Tree
- leetcode 655. Print Binary Tree
- LeetCode 之 DFS 深度优先遍历
- leetcode 104. Maximum Depth of Binary Tree DFS深度优先搜索
- leetcode 98. Validate Binary Search Tree DFS深度优先搜索 + 一个错误做法
- leetcode 124. Binary Tree Maximum Path Sum 最大路径和 + DFS深度优先搜索
- Linux 创建用户 修改用户权限
- mybatis详解-(22)整合第三方缓存Ehcache
- 软件各种版本的含义!例如RC,M,GA等等
- 罗辑思维训练,身为技术人员你的你达到了几级?
- C实现traceroute(MacOS & Linux系统)
- leetcode 655. Print Binary Tree 深度优先遍历DFS
- IntelliJ Idea SpringBoot jpa mysql数据库增删改查实例
- maven详细介绍
- 极光推送集成与封装
- 148. Sort List--单链表自底向上归并排序
- 微信支付,支付宝支付 iOS
- gulp自动添加版本号过程中的一些要点记录
- 深入理解Android之AOP
- Linux下jdk的安装