資料結構 二叉樹面試題(1)

2021-08-22 10:14:04 字數 3521 閱讀 1380

#pragma once

#include #include #include typedef char datatype;

typedef struct btreenode btreenode;

btreenode * createnode(datatype data)

btreenode * createtree(datatype pre, int size, int *index)

if (pre[*index] == '#')

btreenode *proot = createnode(pre[*index]);

(*index)++;

proot->pleft = createtree(pre, size, index);

proot->pright = createtree(pre, size, index);

return proot;

}int getheightr(btreenode *proot)

int left = getheightr(proot->pleft);

int right = getheightr(proot->pright);

return (left > right ? left : right) + 1;

}void getheight(btreenode *proot, int *pheight)

int left, right;

getheight(proot->pleft, &left);

getheight(proot->pright, &right);

*pheight = (left > right ? left : right) + 1;

}int max(int a, int b, int c)

if (b >= a && b >= c)

return c;

}int 找最遠距離(btreenode *proot)

int leftheight = getheightr(proot->pleft);

int rightheight = getheightr(proot->pright);

int rootpath = leftheight + rightheight;

int leftpath = 找最遠距離(proot->pleft);

int rightpath = 找最遠距離(proot->pright);

return max(rootpath, leftpath, rightpath);

}int 找最遠距離優化版(btreenode *proot, int *pheight)

int leftheight, rightheight;

int leftpath = 找最遠距離優化版(proot->pleft, &leftheight);

int rightpath = 找最遠距離優化版(proot->pright, &rightheight);

int rootpath = leftheight + rightheight;

*pheight = (leftheight > rightheight ? leftheight : rightheight) + 1;

return max(rootpath, leftpath, rightpath);

}void mirror(btreenode *proot)

// 可加可不加(加了效率更好)

if (proot->pleft == null && proot->pright == null)

btreenode *pleft = proot->pleft;

proot->pleft = proot->pright;

proot->pright = pleft;

mirror(proot->pleft);

mirror(proot->pright);

}int isbalance(btreenode *proot)

int left = isbalance(proot->pleft);

if (left == 0)

int right = isbalance(proot->pright);

if (right == 0)

// 左右子樹都平衡

int lefth = getheightr(proot->pleft);

int righth = getheightr(proot->pright);

if (lefth - righth >= -1 && lefth - righth <= 1)

else

}btreenode * find(btreenode *proot, btreenode *node)

if (proot == node)

btreenode *pfound = find(proot->pleft, node);

if (pfound != null)

return find(proot->pright, node);

}btreenode * 找最近公共祖先(btreenode *proot, btreenode *n1, btreenode *n2)

if (pn1rightfound != null && pn2rightfound != null)

if (pn1leftfound != null && pn2rightfound != null)

if (pn2leftfound != null && pn1rightfound != null)

if (n1 == proot && (pn2leftfound || pn2rightfound))

if (n2 == proot && (pn1leftfound || pn1rightfound))

return null;

}btreenode * createtreebypreorderandinorder(

char preorder, int preordersize,

char inorder, int inordersize)

char rootchar = preorder[0]; // 這裡越界了麼?

int i;

for (i = 0; i < inordersize; i++)

} if (i == inordersize)

btreenode *proot = createnode(rootchar);

proot->pleft = createtreebypreorderandinorder(

preorder + 1, i,

inorder, i);

proot->pright = createtreebypreorderandinorder(

preorder + 1 + i, preordersize - i - 1,

inorder + 1 + i, inordersize - i - 1);

return proot;

}void test()

資料結構 二叉樹面試題(2)

pragma once include include include typedef struct bstreenode bstreenode bstreenode createnode int data 如果存在,插入失敗返回 1,否則插入成功,返回 0 int insert bstreenod...

資料結構 二叉樹基本操作 二叉樹面試題

二叉樹的基本概念就不多說了 如下 binarytree.c pragma once include include include include typedef char btdatatype typedef struct binarytreenode btnode a是乙個前序遍歷的陣列 二叉樹...

資料結構 二叉樹有關的面試題

node rebuildbtree int preorder,int lenth,int inorder,int start,int end if root left if root right return root bool hascommonroot node roo1,node roo2 r...