平衡二叉树的性质为: 要么是一棵空树,要么任何一个节点的左右子树高度差的绝对值不超过 1。给定一棵二叉树(保证树中的节点值互不相同),判断这棵二叉树是否为平衡二叉树。
一颗树的高度指的是树的根节点到所有节点的距离中的最大值。
第一行输入两个整数 n 和 root,n 表示二叉树的总节点个数,root 表示二叉树的根节点。
以下 n 行每行三个整数 fa,lch,rch,表示 fa 的左儿子为 lch,右儿子为 rch。(如果 lch 为 0 则表示 fa 没有左儿子,rch同理)
如果是平衡二叉树则输出 "true",否则输出 "false"。
3 1 1 2 3 2 0 0 3 0 0
true
6 1 1 2 3 2 4 5 4 6 0 3 0 0 5 0 0 6 0 0
false
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.Queue; import java.util.LinkedList; class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int val) { this.val = val; this.left = null; this.right = null; } } public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] params = br.readLine().trim().split(" "); int n = Integer.parseInt(params[0]); // 先建树 TreeNode tree = buildTree(br); // 然后判断平衡性 System.out.println(isBalanced(tree)); } private static TreeNode buildTree(BufferedReader br) throws IOException { String line = br.readLine(); int[] arr = cast(line); TreeNode head = new TreeNode(arr[0]); if(arr[1] != 0) head.left = buildTree(br); if(arr[2] != 0) head.right = buildTree(br); return head; } private static int[] cast(String str){ String[] strings = str.split(" "); int[] arr = new int[strings.length]; for(int i = 0; i < arr.length; i++) arr[i] = Integer.parseInt(strings[i]); return arr; } private static boolean isBalanced(TreeNode root) { if(root == null) return true; // 空树平衡 if(Math.abs(TreeDepth(root.left) - TreeDepth(root.right)) <= 1) return isBalanced(root.left) && isBalanced(root.right); // 左右子树都要平衡 return false; } private static int TreeDepth(TreeNode tree) { if(tree == null){ return 0; }else return Math.max(TreeDepth(tree.left), TreeDepth(tree.right)) + 1; } }
import java.util.Scanner; public class Main { public static Node[] nodes; public static int n; public static int root; public static class Node { public int val; public int left, right; Node(int val, int left , int right) { this.val = val; this.left = left; this.right = right; } } public static class ReturnVal { public boolean isBalance; public int height; ReturnVal(boolean isBalance, int height) { this.isBalance = isBalance; this.height = height; } } public static ReturnVal isBalance(int index) { if (index == 0) { return new ReturnVal(true, 0); } ReturnVal left = isBalance(nodes[index].left); if (!left.isBalance) return new ReturnVal(false, 0); ReturnVal right = isBalance(nodes[index].right); if (!right.isBalance) return new ReturnVal(false, 0); if (Math.abs(left.height-right.height) > 1) return new ReturnVal(false, 0); int height = Math.max(left.height, right.height) + 1; boolean isBalance = Math.abs(left.height-right.height) <= 1 && left.isBalance & right.isBalance; return new ReturnVal(isBalance, height); } public static void main(String[] args) { Scanner input = new Scanner(System.in); n = input.nextInt(); root = input.nextInt(); nodes = new Node[n+1]; for (int i = 0; i < n; i++) { int val = input.nextInt(); int left = input.nextInt(); int right = input.nextInt(); nodes[val] = new Node(val, left, right); } System.out.println(isBalance(root).isBalance); } }
#include <bits/stdc++.h> using namespace std; struct TreeNode{ int val; TreeNode* lch; TreeNode* rch; TreeNode(int x):val(x),lch(NULL),rch(NULL){} }; void createTree(TreeNode* root, int& cnt){ if(cnt == 0) return; int p, l, r; cin >> p >> l >> r; if(l){ TreeNode* lch = new TreeNode(l); root->lch = lch; createTree(root->lch, --cnt); } if(r){ TreeNode* rch = new TreeNode(r); root->rch = rch; createTree(root->rch, --cnt); } return; } int helper(TreeNode* root){ if(!root) return 0; int leftHight = helper(root->lch); int rightHight = helper(root->rch); if(leftHight == -1 || rightHight == -1) return -1; if(abs(leftHight - rightHight) < 2) return max(leftHight, rightHight)+1; else return -1; } bool judge(TreeNode* root){ if(helper(root) == -1) return false; else return true; } int main(){ int n, root_val; cin >> n >> root_val; TreeNode* root = new TreeNode(root_val); createTree(root, n); if(judge(root)) cout << "true" << endl; else cout << "false" << endl; return 0; }
以下 n 行每行三个整数 fa,lch,rch,表示 fa 的左儿子为 lch,右儿子为 rch。(如果 lch 为 0 则表示 fa 没有左儿子,rch同理)
#include <iostream> #include <math.h> using namespace std; struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode(int x): val(x), left(NULL), right(NULL) {} }; void createTree(TreeNode* root, int& n) { if(n == 0) return ; int rootval, leftval, rightval; cin >> rootval >> leftval >> rightval; if(leftval != 0) { TreeNode* leftNode = new TreeNode(leftval); leftNode->left = leftNode->right = nullptr; root->left = leftNode; createTree(leftNode, --n); } if(rightval != 0) { TreeNode* rightNode = new TreeNode(rightval); rightNode->left = rightNode->right = nullptr; root->right = rightNode; createTree(rightNode, --n); } return ; } int getHeight(TreeNode* root) { if(root == nullptr) return 0; int leftHeight = getHeight(root->left); int rightHeight = getHeight(root->right); int treeHeight = max(leftHeight, rightHeight) + 1; return treeHeight; } bool isBalanced(TreeNode* root) { if(root == nullptr) return true; return isBalanced(root->left) && isBalanced(root->right) && abs(getHeight(root->left) - getHeight(root->right)) <= 1; } int main() { int n, rootval; cin >> n >> rootval; TreeNode* root = new TreeNode(rootval); root->left = root->right = nullptr; createTree(root, n); if(isBalanced(root)) cout << "true" << endl; else cout << "false" << endl; return 0; }
#include <stdio.h> #include <stdlib.h> typedef int Id; typedef struct { Id id; Id l_child, r_child; } TreeNodeInfo, *INFO; typedef struct TreeNode { Id id; struct TreeNode* left; struct TreeNode* right; } TreeNode, *PTNode, *BT; BT CreateBT(INFO infos, Id root_id) { // recursion exit condition if (!root_id) return NULL; BT t = (PTNode) malloc(sizeof(TreeNode)); if (!t) return NULL; t->id = root_id; t->left = CreateBT(infos, infos[root_id].l_child); t->right = CreateBT(infos, infos[root_id].r_child); return t; } int isBalanced(BT t, int* ans) { if (!t) return 0; int l = isBalanced(t->left, ans); int r = isBalanced(t->right, ans); if (abs(l - r) > 1) *ans = 0; return 1 + fmax(l, r); } int main(const int argc, const char* const argv[]) { int n, root_id; fscanf(stdin, "%d %d", &n, &root_id); TreeNodeInfo infos[n + 1]; Id fa, l_ch, r_ch; while (n--) { fscanf(stdin, "%d %d %d", &fa, &l_ch, &r_ch); infos[fa].id = fa; infos[fa].l_child = l_ch; infos[fa].r_child = r_ch; } int ans = 1; isBalanced(CreateBT(infos, root_id), &ans); fputs(ans ? "true" : "false", stdout); return 0; }
#判断以root为根节点的树nodes是否是平衡树 def is_balance(root, nodes): #如果树为空 #树高为0 #是平衡树 if root == 0: return 0, True #如果树不为空 #分别求左右树高 #判断左右子树是否为平衡树 l_h, l_B = is_balance(nodes[root]["l"], nodes) r_h, r_B = is_balance(nodes[root]["r"], nodes) #接下来判断以root为根的树是否为平衡树 #先假设不是 is_B = False #求树高 rt_h = max(l_h, r_h) + 1 #如果左右子树是平衡树 if l_B and r_B: #根据平衡树的定义判断 if abs(l_h - r_h) <= 1: is_B = True return rt_h, is_B #树的总节点数,根 n, root = map(int,input().strip().split(' ')) #树 tree={} for i in range(n): root,l,r=map(int,input().strip().split(' ')) tree[root]={"l":l,"r":r} _, is_B = is_balance(root, tree) if is_B: print("true") else: print("false")树的题目注意利用递归。
#include<iostream> using namespace std; struct TreeNode{ int val; TreeNode* left; TreeNode* right; }; void CreateTree(TreeNode* pRoot,int& n){ if(n==0){ return; } int rootval,leftval,rightval; cin>>rootval>>leftval>>rightval; if(leftval!=0){ TreeNode* leftnode = new TreeNode; leftnode->val = leftval; leftnode->left=leftnode->right=nullptr; pRoot->left = leftnode; CreateTree(leftnode,--n); } if(rightval!=0){ TreeNode* rightnode = new TreeNode; rightnode->val = rightval; rightnode->left=rightnode->right=nullptr; pRoot->right = rightnode; CreateTree(rightnode,--n); } } struct ReturnType{ bool isbalanced; int height; ReturnType(bool _isbalanced,int _height){ isbalanced = _isbalanced; height = _height; } }; ReturnType process(TreeNode* pRoot){ if(pRoot==nullptr){ ReturnType a(true,0); return a; } ReturnType leftData = process(pRoot->left); ReturnType rightData = process(pRoot->right); bool isbalanced =leftData.isbalanced && rightData.isbalanced &&(abs(leftData.height-rightData.height)<2); int height = max(leftData.height,rightData.height)+1; ReturnType res(isbalanced,height); return res; } int main(){ int n,rootval; cin>>n>>rootval; TreeNode* pRoot = new TreeNode; pRoot->val = rootval; pRoot->left = pRoot->right = nullptr; TreeNode* pCurrNode = pRoot; CreateTree(pCurrNode,n); ReturnType res = process(pRoot); if(res.isbalanced){ cout<<"true"<<endl; }else{ cout<<"false"<<endl; } return 0; }
#include<bits/stdc++.h> using namespace std; // 法一 int getHeight(int root,int* lc,int* rc) { if(!root) return 0; return 1 + max(getHeight(lc[root],lc,rc),getHeight(rc[root],lc,rc)); } bool judge(int root,int*lc, int* rc) { if(!root) return true; int l = getHeight(lc[root],lc,rc); int r = getHeight(rc[root],lc,rc); if(abs(l-r)>=2) return false; return true; } bool visit(int root,int* lc,int* rc) { if(!root) return true; return judge(root,lc,rc) && visit(lc[root],lc,rc) && visit(rc[root],lc,rc); } ///// 改进,法二 树形dp // 需要子树返回的信息 // 是否平衡 以及树高 struct ReturnType{ bool isBalanced; int height; ReturnType(bool a,int b):isBalanced(a),height(b){} }; ReturnType process(int root,int* lc,int* rc) { // 空树是平衡树 if(!root) return ReturnType(true,0); ReturnType l = process(lc[root],lc,rc); ReturnType r = process(rc[root],lc,rc); int cur_height = max(l.height,r.height) + 1; // 以当前结点为根的树平衡的条件:左右子树都平衡 且 左右子树的树高差值最多为1 bool cur_balance = l.isBalanced && r.isBalanced && abs(l.height-r.height)<2 ? true : false; return ReturnType(cur_balance,cur_height); } int main() { int n,root; cin>>n>>root; int* lc = new int[n+1]; int* rc = new int[n+1]; int p; for(int i=0;i<n;++i) { cin>>p; cin>>lc[p]>>rc[p]; } //if(visit(root,lc,rc)) if(process(root,lc,rc).isBalanced) cout<<"true"; else cout<<"false"; return 0; }
int isBalanceTree(TreeNode *root) { if (root == nullptr) return 0; // 判断边界 int leftHeight = isBalanceTree(root->left); int rightHeight = isBalanceTree(root->right); if (leftHeight == -1 || rightHeight == -1) return -1; if (abs(leftHeight - rightHeight) < 2) return max(leftHeight, rightHeight) + 1; else return -1; } bool isBalanced(TreeNode* root) { if (isBalanceTree(root) != -1) { return true; } else return false; }