面试笔试杂项积累-leetcode 106-110
2016-02-04 23:21
465 查看
博主今天一天做了19道题,嗯,可喜可贺
For example:
Given binary tree
return its bottom-up level order traversal as:
102是从头到底,这道是从底到头,
博主的方法有点麻烦,求出最长深度再减
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
IList<IList<int>> fin = new List<IList<int>>();
int height = 0;
public IList<IList<int>> LevelOrderBottom(TreeNode root)
{
if (root == null)
return fin;
height = getDeep(root);
for (int i = 0; i < height; i++)
{
fin.Add(new List<int>());
}
traverse(root, 0);
return fin;
}
public int getDeep(TreeNode temp)
{
int deep = 0;
if (temp != null)
{
int lDeep = getDeep(temp.left);
int rDeep = getDeep(temp.right);
deep = lDeep > rDeep ? lDeep + 1 : rDeep + 1;
}
return deep;
}
void traverse(TreeNode root, int level)
{
if (root.left != null)
traverse(root.left, level + 1);
fin[height - level - 1].Add(root.val);
if (root.right != null)
traverse(root.right, level + 1);
}
}
108.108-Convert Sorted Array to Binary Search Tree-Difficulty: Medium
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
因为是排好序的数组所以就很简单了,用递归来做,每次递归的中间节点就是根节点
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode SortedArrayToBST(int[] nums) {
if (nums.Length == 0)
return null;
return recursion(nums, 0, nums.Length - 1);
}
TreeNode recursion(int[] nums, int start, int end)
{
if (start > end)
return null;
int mid = (start + end) / 2;
TreeNode root = new TreeNode(nums[mid]);
root.left = recursion(nums, start, mid - 1);
root.right = recursion(nums, mid + 1, end);
return root;
}
}
109.109-Binary Tree Level Order Traversal II-Difficulty: Medium
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
方法一
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode SortedListToBST(ListNode head) {
if (head == null)
return null;
IList<int> list = new List<int>();
ListNode temp = head;
while (temp != null)
{
list.Add(temp.val);
temp = temp.next;
}
return recursion(list, 0, list.Count - 1);
}
public TreeNode SortedArrayToBST(IList<int> nums)
{
return recursion(nums, 0, nums.Count - 1);
}
TreeNode recursion(IList<int> nums, int start, int end)
{
if (start > end)
return null;
int mid = (start + end) / 2;
TreeNode root = new TreeNode(nums[mid]);
root.left = recursion(nums, start, mid - 1);
root.right = recursion(nums, mid + 1, end);
return root;
}
}
方法二
有点像跳表那样,两个节点,一个fast一个正常,在循环中fast每次跳两个节点,正常走一个节点。当fast到终点了,正常的也就正好到一半了。
参考:
http://www.cnblogs.com/ganganloveu/p/4061909.html
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *sortedListToBST(ListNode *head) {
if(head == NULL)
return NULL;
else if(head->next == NULL)
return new TreeNode(head->val);
ListNode* mid = findMid(head);
TreeNode* root = new TreeNode(mid->val);
root->left = sortedListToBST(head);
root->right = sortedListToBST(mid->next);
return root;
}
ListNode* findMid(ListNode* head)
{
ListNode* preslow = NULL;
ListNode* slow = head;
ListNode* fast = head;
while(fast != NULL)
{
fast = fast->next;
if(fast != NULL)
{
fast = fast->next;
preslow = slow;
slow = slow->next;
}
}
//break the list into two parts
preslow->next = NULL;
return slow;
}
};
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of
every node never differ by more than 1.
这个很简单,深度遍历,先序中序后序都可以,求出当前节点左右儿子的深度,如果相差大于1直接return false,全比一遍没有超1的,就是true。递归实现.
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public bool IsBalanced(TreeNode root) {
if (root == null)
return true;
return printTreeDepth1(root);
}
public int getDeep(TreeNode temp)
{
int deep = 0;
if (temp != null)
{
int lDeep = getDeep(temp.left);
int rDeep = getDeep(temp.right);
deep = lDeep > rDeep ? lDeep + 1 : rDeep + 1;
}
return deep;
}
public bool printTreeDepth1(TreeNode temp)//递归的深度遍历-先序
{
if (Math.Abs(getDeep(temp.left) - getDeep(temp.right)) > 1)
return false;
if (temp.left != null)
{
if (!printTreeDepth1(temp.left))
return false;
}
if (temp.right != null)
{
if (!printTreeDepth1(temp.right))
return false;
}
return true;
}
}
107.107-Binary Tree Level Order Traversal II-Difficulty: Easy
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:
Given binary tree
{3,9,20,#,#,15,7},
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3] ]
思路
层序遍历102是从头到底,这道是从底到头,
博主的方法有点麻烦,求出最长深度再减
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
IList<IList<int>> fin = new List<IList<int>>();
int height = 0;
public IList<IList<int>> LevelOrderBottom(TreeNode root)
{
if (root == null)
return fin;
height = getDeep(root);
for (int i = 0; i < height; i++)
{
fin.Add(new List<int>());
}
traverse(root, 0);
return fin;
}
public int getDeep(TreeNode temp)
{
int deep = 0;
if (temp != null)
{
int lDeep = getDeep(temp.left);
int rDeep = getDeep(temp.right);
deep = lDeep > rDeep ? lDeep + 1 : rDeep + 1;
}
return deep;
}
void traverse(TreeNode root, int level)
{
if (root.left != null)
traverse(root.left, level + 1);
fin[height - level - 1].Add(root.val);
if (root.right != null)
traverse(root.right, level + 1);
}
}
108.108-Convert Sorted Array to Binary Search Tree-Difficulty: Medium
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路
给予一个排好序的数组,返回一个平衡二叉搜索树因为是排好序的数组所以就很简单了,用递归来做,每次递归的中间节点就是根节点
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode SortedArrayToBST(int[] nums) {
if (nums.Length == 0)
return null;
return recursion(nums, 0, nums.Length - 1);
}
TreeNode recursion(int[] nums, int start, int end)
{
if (start > end)
return null;
int mid = (start + end) / 2;
TreeNode root = new TreeNode(nums[mid]);
root.left = recursion(nums, start, mid - 1);
root.right = recursion(nums, mid + 1, end);
return root;
}
}
109.109-Binary Tree Level Order Traversal II-Difficulty: Medium
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.方法一
思路
与上一道题不同的是,本次给的是排好序的链表,比上一道题的数组麻烦一些,博主直接把链表排成数组再利用上一道题,比用链表找中点省事多了,但后来想想,失去了锻炼使用链表的机会。。。遂补上链表方法/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode SortedListToBST(ListNode head) {
if (head == null)
return null;
IList<int> list = new List<int>();
ListNode temp = head;
while (temp != null)
{
list.Add(temp.val);
temp = temp.next;
}
return recursion(list, 0, list.Count - 1);
}
public TreeNode SortedArrayToBST(IList<int> nums)
{
return recursion(nums, 0, nums.Count - 1);
}
TreeNode recursion(IList<int> nums, int start, int end)
{
if (start > end)
return null;
int mid = (start + end) / 2;
TreeNode root = new TreeNode(nums[mid]);
root.left = recursion(nums, start, mid - 1);
root.right = recursion(nums, mid + 1, end);
return root;
}
}
方法二
思路
和上题相同依旧通过找到中点作为根节点遍历即可,重点说明如何找到链表的中间节点有点像跳表那样,两个节点,一个fast一个正常,在循环中fast每次跳两个节点,正常走一个节点。当fast到终点了,正常的也就正好到一半了。
参考:
http://www.cnblogs.com/ganganloveu/p/4061909.html
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *sortedListToBST(ListNode *head) {
if(head == NULL)
return NULL;
else if(head->next == NULL)
return new TreeNode(head->val);
ListNode* mid = findMid(head);
TreeNode* root = new TreeNode(mid->val);
root->left = sortedListToBST(head);
root->right = sortedListToBST(mid->next);
return root;
}
ListNode* findMid(ListNode* head)
{
ListNode* preslow = NULL;
ListNode* slow = head;
ListNode* fast = head;
while(fast != NULL)
{
fast = fast->next;
if(fast != NULL)
{
fast = fast->next;
preslow = slow;
slow = slow->next;
}
}
//break the list into two parts
preslow->next = NULL;
return slow;
}
};
110.110-Balanced Binary Tree-Difficulty: Easy
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of
every node never differ by more than 1.
思路
判断给定二叉树是否高度平衡,这个很简单,深度遍历,先序中序后序都可以,求出当前节点左右儿子的深度,如果相差大于1直接return false,全比一遍没有超1的,就是true。递归实现.
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public bool IsBalanced(TreeNode root) {
if (root == null)
return true;
return printTreeDepth1(root);
}
public int getDeep(TreeNode temp)
{
int deep = 0;
if (temp != null)
{
int lDeep = getDeep(temp.left);
int rDeep = getDeep(temp.right);
deep = lDeep > rDeep ? lDeep + 1 : rDeep + 1;
}
return deep;
}
public bool printTreeDepth1(TreeNode temp)//递归的深度遍历-先序
{
if (Math.Abs(getDeep(temp.left) - getDeep(temp.right)) > 1)
return false;
if (temp.left != null)
{
if (!printTreeDepth1(temp.left))
return false;
}
if (temp.right != null)
{
if (!printTreeDepth1(temp.right))
return false;
}
return true;
}
}
相关文章推荐
- 面试笔试杂项积累-leetcode 101-105
- 面试笔试杂项积累-leetcode 96-100
- 11个提问频率最高的PHP面试题
- PHP新浪面试题及完整答案
- 闲聊:项目延期,怎么回事?
- 职场新人 别人凭什么要教你
- 如何成为优秀的Java程序员
- 哪种锻炼方式最能让程序员远离亚健康? - 强烈推荐
- Java 面试题
- 面试:字符串集合 查找包含算法 的素数方案
- QA 面试问题汇总
- 2016年,新的开始
- Java面试题目整理
- Android工程师面试题大全
- 面试必问问题最佳答案
- 只有程序员才会下载的黄片儿
- 笔试题&面试题:找出一个数组中第m小的值并输出
- 做程序员的老婆应该注意的一些事情
- 关于App程序员泡沫
- 码农十年连载五