This program is to reconstruct a binary tree according to its preorder and inorder traversals.
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
if (pre==null)
return null;
TreeNode treeNode = new TreeNode(pre[0]);
int i = 0;
while (i<in.length&&in[i]!=pre[0])
++i;
if (i>=in.length) {
return null;
}
if (i>=1) {
int[] preleft = new int[i];
int[] inleft = new int[i];
for(int j=0;j<i;++j) {
preleft[j] = pre[j+1];
inleft[j] = in[j];
}
treeNode.left = reConstructBinaryTree(preleft, inleft);
} else {
treeNode.left = null;
}
if (i<pre.length-1) {
int[] preright = new int[pre.length-i-1];
int[] inright = new int[pre.length-i-1];
for(int j=0;j<pre.length-i-1;++j) {
preright[j] = pre[i+j+1];
inright[j] = in[i+j+1];
}
treeNode.right = reConstructBinaryTree(preright, inright);
} else {
treeNode.right = null;
}
return treeNode;
}
}
Thursday, August 18, 2016
Wednesday, August 17, 2016
Java Program: Replace Space With "%20" In A String
Here is a java program to replace space with "%20" in a string. For example, the string "We Are Happy" will be changed to "We%20Are%20Happy".
Note that a space is a character while "%20" is a string.
//Replace Space With "%20" In A String
public class Solution {
public String replaceSpace(StringBuffer str) {
StringBuffer s = new StringBuffer();
for(int i=0;i<str.length();++i) {
if (str.charAt(i)==' ') {
s.append("%20");
} else {
s.append(str.charAt(i));
}
}
return s.toString();
}
}
Note that a space is a character while "%20" is a string.
//Replace Space With "%20" In A String
public class Solution {
public String replaceSpace(StringBuffer str) {
StringBuffer s = new StringBuffer();
for(int i=0;i<str.length();++i) {
if (str.charAt(i)==' ') {
s.append("%20");
} else {
s.append(str.charAt(i));
}
}
return s.toString();
}
}
Monday, August 8, 2016
Android Studio Project: Outside News
The previous projects I have made are mainly about logical thinking. Today, I share this project, Outside News to all of you, which is full of beautiful user interfaces. I'm sorry that it's only a demo without news recources and strong features. If you want to have a try, just do it!
Installation package
Project site
Is this project with the use of Viewpager, ListView, PopupWindow, PopupMenu and many other views more colorful? What's more, it uses SharedPreferences and SQLite database to store some data persistently. And it involves multithread programming and asynchronous message processing. The use of vector is also one of my harvests.
Installation package
Project site
Is this project with the use of Viewpager, ListView, PopupWindow, PopupMenu and many other views more colorful? What's more, it uses SharedPreferences and SQLite database to store some data persistently. And it involves multithread programming and asynchronous message processing. The use of vector is also one of my harvests.
Sunday, August 7, 2016
Android Learning Note: To Set The Color Of Cursor The Same As Text Color In An EditText
To set the color of cursor the same as text color in an EditText, just add a sentence in the layout file like this:
android:textCursorDrawable="@null"
"@null" here means the color of cursor is the same with the text. It's said that you can change it as you like to set the color arbitrarily. You may have a try.
android:textCursorDrawable="@null"
"@null" here means the color of cursor is the same with the text. It's said that you can change it as you like to set the color arbitrarily. You may have a try.
Subscribe to:
Posts (Atom)