CTCI Chapter 4 | Trees and Graphs | 叠黛
13 九月 2015 九月 13, 2015 int checkHeight(TreeNode *root) { if (!root) return 0; int heightL = checkHeight(root->left); if (hrightL == -1) return -1; int heightR = checkHeight(root->right); if (hrightR == -1) return -1; if (abs(heightR - heightL) > 1) return -1; else return max(heightR, heightL) + 1; } bool isBalanced(TreeNode *root) { if (checkHeight(root) == -1) return false; else return true; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 int heightR = checkHeight(root->right); if (abs(heightR - heightL) > 1) return -1; else return max(heightR, heightL) + 1; } else return true; } 4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nodes. 4.3 Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height. 4.4 Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (i.e., if you have a tree with depth D, you’ll have D linked lists).Read full article from CTCI Chapter 4 | Trees and Graphs | 叠黛
No comments:
Post a Comment